I want to show time in 12 hours format without using the AM
and PM
. For example 3:45
only and not 3:45 PM
or 3:45 AM
. How I can modify the toLocaleTimeString()
to not show the PM
AM
but in 12 number format?
var minsToAdd = 45;
var time = "15:00";
var newTime = new Date(new Date("2000/01/01 " + time).getTime() + minsToAdd * 60000).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: true });
console.log(newTime);
I want to show time in 12 hours format without using the AM
and PM
. For example 3:45
only and not 3:45 PM
or 3:45 AM
. How I can modify the toLocaleTimeString()
to not show the PM
AM
but in 12 number format?
var minsToAdd = 45;
var time = "15:00";
var newTime = new Date(new Date("2000/01/01 " + time).getTime() + minsToAdd * 60000).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: true });
console.log(newTime);
Share
Improve this question
edited Apr 2, 2019 at 21:43
halfer
20.4k19 gold badges108 silver badges201 bronze badges
asked Apr 2, 2019 at 15:38
Mona CoderMona Coder
6,31618 gold badges70 silver badges137 bronze badges
5
|
4 Answers
Reset to default 8.toLocaleTimeString() did not have any override to do so.
There are multiple ways to do so.
Replace AM/PM by blank:
var minsToAdd = 45;
var time = "15:00";
var newTime = new Date(new Date("2000/01/01 " + time).getTime() + minsToAdd * 60000).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: true });
console.log(newTime.replace("AM","").replace("PM",""));
Using custom JavaScript function:
function formatTime(d) {
function z(n){return (n<10?'0':'')+n}
var h = d.getHours();
return (h%12 || 12) + ':' + z(d.getMinutes());
}
var minsToAdd = 45;
var time = "15:00";
var newTime = new Date(new Date("2000/01/01 " + time).getTime() + minsToAdd * 60000);
console.log(formatTime(newTime));
it's very ez.
const date24 = new Date();
const data24Time = date24.toLocaleTimeString('en-IT', { hour12: false })
console.log("24 h : ",data24Time)
// 24 h : 20:26:09
const date12 = new Date();
const data12Time = date12.toLocaleTimeString('en-IT')
console.log("12 h : ",data12Time)
// 12 h : 8:26:09 PM
// toLocaleTimeString('{languege for show time}-{languege for set}')
formats below assume the local time zone of the locale; America/Los_Angeles for the US
US English uses 12-hour time with AM/PM console.log(date.toLocaleTimeString('en-US')); "7:00:00 PM"
For more information, visit official docs here
Okay so the easiest way to do so is by using formatting conventions
just replace 'en-US' with 'it-IT' in your code
newTime
? – 31piy Commented Apr 2, 2019 at 15:40toLocalTimeString()
– Mona Coder Commented Apr 2, 2019 at 15:41