47

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.

Manish Singh
  • 5,848
  • 4
  • 43
  • 31
Kishh
  • 2,005
  • 4
  • 19
  • 16
  • 7
    Can you describe in words how you would solve this problem? (This would be to determine whether you understand the problem itself.) – Greg Hewgill Nov 24 '09 at 08:40
  • 1
    Here are useful answers for this: http://stackoverflow.com/questions/22347521/change-time-format-to-24-hours-in-javascript – Andrew May 06 '16 at 05:55

2 Answers2

36
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
}
SheetJS
  • 22,470
  • 12
  • 65
  • 75
6

In moment.js you can do:

theDate.format("H:MM")

Take a look here for more details: http://blog.stevenlevithan.com/archives/date-time-format

Konamiman
  • 49,681
  • 17
  • 108
  • 138