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.

Advert

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.

Comments

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. How to Remove an App from Django Project

     
  2. Parse Time HH:mm:ss without date in javascript

     
  3. Software Testing

     
  4. Best WordPress Themes For Photographers And Photography

     
  5. Problems That Affect Conversion Rates

     

News Letter

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