Evan
Evan

Reputation: 163

Javascript time string to date

I have a time as a string like "10:00pm" and I need to convert it into a date object so it can be represented in an input type="time" tag. Im using angularjs to model it.

I get an error saying "Expected 10:00am to be a date". How can I convert "10:00am" into a date object? I haven't been able to figure it out. Any help is appreciated.

I only need the time

Upvotes: 1

Views: 88

Answers (2)

ksav
ksav

Reputation: 20840

Have you seen http://momentjs.com/ ?

You can use it like this:

var time = moment("10:00pm", "HH:mm a");

Upvotes: 1

Richard Austin
Richard Austin

Reputation: 26

If you're using date.js then you could try

Date.parseExact("10:00 PM", "hh:mm tt");

This should also pick up if you've loaded the library correctly.

Upvotes: 0

Related Questions