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.
Last 5 Articles
All Articles >
-
Data Protection Laws Around the World
-
NodeJS Overview
-
Server Side Render ReactJS Application
-
How to Deploy React JS application
-
Environment Variables in React JS
News Letter
Subscribe to our email newsletter for useful tips and valuable resources, sent out every new article release.
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.