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 + Formatparsing 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(, this will work with 12 or 24 hour times.
Moment('14:00:00', 'HH:mm:ss')
Refer Doc for accepted formats.