How can I isolate and convert the local time format in JavaScript's new Date()
to a 24 hour format?
Example: 2016-09-22T23:21:56.027Z
just becomes 22:56
in local time.
let dt = moment(); dt. format(“HH:mm”) // 24 hour time.
Convert the 24 hours format time to 12 hours formatted time. Now in-order to convert it to 12 hours format you can take % 12 on the current time. time = 24, then 24%12 → 0 , if the time is 0, then change the time as 12.
A time of day is written in the 24-hour notation in the form hh:mm (for example 01:23) or hh:mm:ss (for example, 01:23:45), where hh (00 to 23) is the number of full hours that have passed since midnight, mm (00 to 59) is the number of full minutes that have passed since the last full hour, and ss (00 to 59) is the ...
new Date("2000-01-01 10:30 AM").getHours() // 10
This is 24 hours:
new Date("2000-01-01 10:30 PM").getHours() // 22
If you want a more general thing:
function get_hours(time_string) { return new Date("2000-01-01 " + time_string).getHours() // 22 }
theDate.format("H:MM")
Take a look here for more details: http://blog.stevenlevithan.com/archives/date-time-format
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