Parse Time HH:mm:ss without date in javascript

How to parse only a Time string HH:mm:ss in javascript without date or as a date time field.


Unfortunately, there's not a great solution. JavaScript only has a Date object, which is misnamed since it is really a date+time. Which means it cannot parse a time string HH:mm:ss without a date, but it can parse a date string.

There are a few options.

  • Convert time to a datetime string and parse using Date().
  • Use Momentjs String + Format parsing function.

Using Date()

You can add up any date(MM-DD-YYYY) and convert the time(HH:mm:ss) sting to a datetime (MM-DD-YYYY HH:mm:ss) string.

var timeString = '12:23:00';
var datetime = new Date('1970-01-01T' + timeString + 'Z');

1970-01-01T is a sample date to complete the string, you can any date of your choice but make sure u don't use the date part accidentally.

With momentjs

Here you can parse any date, time or datetime provided you know the format. moment(<String>, <String>);, this will work with 12 or 24 hour times.

Moment('14:00:00', 'HH:mm:ss')

Refer Doc for accepted formats.


Wow ! you have someting to tell us. That's great! Please keep in mind that comments are moderated, we employ rel="nofollow" for links, avoid using a spammy word or a domain in name field, it might end up as a Spam. Thanks for reading.

Last 5 Articles

All Articles >

  1. Security Myths

  2. Model Design for Orders in Django

  3. Automating Business Operations

  4. Unit Testing in Python using assertAlmostEqual

  5. 3 Facebook Tips and Tricks Your Local Business Will Benefit


News Letter

Subscribe to our email newsletter for useful tips and valuable resources, sent out every new article release.