Add “am” or “pm” to Specific Time Format Dependent on Time of Day - Javascript
I have the following javaScript which is showing the time in hours and minutes.
Is there a way of having an "am" or "pm" next to the time, dependent on whether it's before or after midday?
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.getHours() + ":" + t.getMinutes();
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
javascript time
add a comment |
I have the following javaScript which is showing the time in hours and minutes.
Is there a way of having an "am" or "pm" next to the time, dependent on whether it's before or after midday?
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.getHours() + ":" + t.getMinutes();
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
javascript time
Possible duplicate: stackoverflow.com/questions/14638018/…
– Chris Happy
Nov 26 '18 at 2:06
add a comment |
I have the following javaScript which is showing the time in hours and minutes.
Is there a way of having an "am" or "pm" next to the time, dependent on whether it's before or after midday?
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.getHours() + ":" + t.getMinutes();
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
javascript time
I have the following javaScript which is showing the time in hours and minutes.
Is there a way of having an "am" or "pm" next to the time, dependent on whether it's before or after midday?
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.getHours() + ":" + t.getMinutes();
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.getHours() + ":" + t.getMinutes();
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.getHours() + ":" + t.getMinutes();
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
javascript time
javascript time
asked Nov 26 '18 at 1:59
Paul12_Paul12_
3811211
3811211
Possible duplicate: stackoverflow.com/questions/14638018/…
– Chris Happy
Nov 26 '18 at 2:06
add a comment |
Possible duplicate: stackoverflow.com/questions/14638018/…
– Chris Happy
Nov 26 '18 at 2:06
Possible duplicate: stackoverflow.com/questions/14638018/…
– Chris Happy
Nov 26 '18 at 2:06
Possible duplicate: stackoverflow.com/questions/14638018/…
– Chris Happy
Nov 26 '18 at 2:06
add a comment |
2 Answers
2
active
oldest
votes
Answer adapted from here.
var d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
hr -= 12;
ampm = "pm";
}
var time = document.getElementById("time");
time.textContent = hr + ":" + min + ampm;
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
add a comment |
use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"
for example:
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.toLocaleTimeString('en-US');
source:
https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473877%2fadd-am-or-pm-to-specific-time-format-dependent-on-time-of-day-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Answer adapted from here.
var d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
hr -= 12;
ampm = "pm";
}
var time = document.getElementById("time");
time.textContent = hr + ":" + min + ampm;
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
add a comment |
Answer adapted from here.
var d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
hr -= 12;
ampm = "pm";
}
var time = document.getElementById("time");
time.textContent = hr + ":" + min + ampm;
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
add a comment |
Answer adapted from here.
var d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
hr -= 12;
ampm = "pm";
}
var time = document.getElementById("time");
time.textContent = hr + ":" + min + ampm;
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
Answer adapted from here.
var d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
hr -= 12;
ampm = "pm";
}
var time = document.getElementById("time");
time.textContent = hr + ":" + min + ampm;
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
var d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
hr -= 12;
ampm = "pm";
}
var time = document.getElementById("time");
time.textContent = hr + ":" + min + ampm;
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
var d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
hr -= 12;
ampm = "pm";
}
var time = document.getElementById("time");
time.textContent = hr + ":" + min + ampm;
<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>
answered Nov 26 '18 at 2:08
Chris HappyChris Happy
4,62811234
4,62811234
add a comment |
add a comment |
use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"
for example:
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.toLocaleTimeString('en-US');
source:
https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp
add a comment |
use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"
for example:
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.toLocaleTimeString('en-US');
source:
https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp
add a comment |
use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"
for example:
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.toLocaleTimeString('en-US');
source:
https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp
use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"
for example:
var t = new Date();
var time = document.getElementById("time");
time.textContent = t.toLocaleTimeString('en-US');
source:
https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp
answered Nov 26 '18 at 2:07
hortighortig
854
854
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473877%2fadd-am-or-pm-to-specific-time-format-dependent-on-time-of-day-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate: stackoverflow.com/questions/14638018/…
– Chris Happy
Nov 26 '18 at 2:06