I have a Javascript in which I need to paste the current time in a format HH:MM AM/PM. There's one catch - I need to put the time that starts in two hours from now, so for example, instead of 7:23PM I need to put 9:23PM, etc.
I tried to do something like: var dateFormat = new Date("hh:mm a")
but it didn't work. I also tried to use:
var today = new Date();
var time = today.toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
alert(time);
but all I've seen was e.g. 18:23 instead of 6:23 PM (probably because of toLocaleTimeString() and my location in Europe) - maybe there's some unified way to do that that will work all around the World?. Also, I don't know exactly how to add the 2 hours to the final result. Can you help me? Thanks!
Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM. aa – AM/PM marker. In this example we are displaying current date and time with AM/PM marker.
You should use ddd for an abbreviation of the name of day of the week, DD for day of the month, MMM for an abbreviation of the month's name, YYYY for the year, hh for the 1-12 hour, mm for minutes and A for AM/PM . See moment(String, String) docs. Save this answer.
The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.
Here is how you can get the time in “h:i:s” format. You can change the format whatever you wish. let currentDate = new Date (); let time = currentDate.getHours () + ":" + currentDate.getMinutes () + ":" + currentDate.getSeconds (); console.log (time); getHours () – Provides the current hour from 0 to 23.
The Date object is used to get the current time in JavaScript. Here is how you can get the time in “h:i:s” format. You can change the format whatever you wish. getHours () – Provides the current hour from 0 to 23. getMinutes () – Provides the current minutes from 0 to 59.
1. Display current date and time in 12 hour format with AM/PM. There are two patterns that we can use in SimpleDateFormat to display time. Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM.
1. Display current date and time in 12 hour format with AM/PM There are two patterns that we can use in SimpleDateFormat to display time. Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM.
You can convert the current time to 12 hour format with a one liner
new Date().toLocaleTimeString('en-US', { hour: 'numeric', hour12: true, minute: 'numeric' });
And to add two hours to your current time
Date.now() + 2 * 60 * 60 * 1000
So you can do it in a simple one line as:
new Date(Date.now() + 2 * 60 * 60 * 1000).toLocaleTimeString('en-US', { hour: 'numeric', hour12: true, minute: 'numeric' });
Use Date
methods to set and retrieve time and construct a time string, something along the lines of the snippet.
[edit] Just for fun: added a more generic approach, using 2 Date.prototype
extensions.
var now = new Date();
now.setHours(now.getHours()+2);
var isPM = now.getHours() >= 12;
var isMidday = now.getHours() == 12;
var result = document.querySelector('#result');
var time = [now.getHours() - (isPM && !isMidday ? 12 : 0),
now.getMinutes(),
now.getSeconds() || '00'].join(':') +
(isPM ? ' pm' : 'am');
result.innerHTML = 'the current time plus two hours = '+ time;
// a more generic approach: extend Date
Date.prototype.addTime = addTime;
Date.prototype.showTime = showTime;
result.innerHTML += '<h4>using Date.prototype extensions</h4>';
result.innerHTML += 'the current time plus twenty minutes = '+
new Date().addTime({minutes: 20}).showTime();
result.innerHTML += '<br>the current time plus one hour and twenty minutes = '+
new Date().addTime({hours: 1, minutes: 20}).showTime();
result.innerHTML += '<br>the current time <i>minus</i> two hours (format military) = '+
new Date().addTime({hours: -2}).showTime(true);
result.innerHTML += '<br>the current time plus ten minutes (format military) = '+
new Date().addTime({minutes: 10}).showTime(true);
function addTime(values) {
for (var l in values) {
var unit = l.substr(0,1).toUpperCase() + l.substr(1);
this['set' + unit](this['get' + unit]() + values[l]);
}
return this;
}
function showTime(military) {
var zeroPad = function () {
return this < 10 ? '0' + this : this;
};
if (military) {
return [ zeroPad.call(this.getHours()),
zeroPad.call(this.getMinutes()),
zeroPad.call(this.getSeconds()) ].join(':');
}
var isPM = this.getHours() >= 12;
var isMidday = this.getHours() == 12;
return time = [ zeroPad.call(this.getHours() - (isPM && !isMidday ? 12 : 0)),
zeroPad.call(this.getMinutes()),
zeroPad.call(this.getSeconds()) ].join(':') +
(isPM ? ' pm' : ' am');
}
<div id="result"></div>
Simply, you can do this
const date = new Date()
const options = {
hour: 'numeric',
minute: 'numeric',
hour12: true
};
const time = new Intl.DateTimeFormat('en-US', options).format(date)
console.log(time)
For more details, you can refer to the MDN docs regarding the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With