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

7
  • 1
    see stackoverflow.com/questions/5619202/… Commented Jul 12, 2016 at 20:57
  • 10:00am will need a day, month and year to become a date Commented Jul 12, 2016 at 20:57
  • I don't need the date. only the time Commented Jul 12, 2016 at 20:58
  • if you have already loaded moment, i would use that. Commented Jul 12, 2016 at 20:58
  • hmmm maybe I shouldn't use an input type="time" tag Commented Jul 12, 2016 at 20:58

2 Answers 2

1

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

You can use it like this:

var time = moment("10:00pm", "HH:mm a");
Sign up to request clarification or add additional context in comments.

1 Comment

+1 if you have already loaded Moment, I would use Moment for any date manipulation, it's just safer that way.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.