1

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

Evan
  • 163
  • 1
  • 1
  • 11

2 Answers2

1

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

You can use it like this:

var time = moment("10:00pm", "HH:mm a");
ksav
  • 20,015
  • 6
  • 46
  • 66
  • 1
    +1 if you have already loaded Moment, I would use Moment for any date manipulation, it's just safer that way. – AJ Meyghani Jul 12 '16 at 21:00
0

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.