0

I am looking for a library that can parse time from given user input. Proper time formats are: hh:mm, BUT optionally it could be AM or PM at the end to specify if it is 12h or 24h format.

The result I want to achieve is to get the time and merge it with predefined date (without time). Our app should parse time without knowing exact culture of the user. Besides: Are there any other proper time formats?

I tried moment.js, but with no luck. It would be almost fine but I would need to manually check if user entered am/pm or not.

Some more info:

User can enter: "8:36 PM", "8:36", "14:00". All those values should be parsed to store time in 24h format.

When user enters "14:00 PM" parsing should fail (I should display validation error)

I know I could parse this by some regexp or sth, but shouldn't be there any ready to go libraries?

mbudnik
  • 2,087
  • 15
  • 34
  • How are you getting the time from the user? Perhaps force them to enter the time in the way **you** want. – Lee Taylor Oct 10 '13 at 20:17
  • I want to have 24h format. But I also want to tolerate users who are entering in 12h format with AM/PM (why not) – mbudnik Oct 10 '13 at 20:20
  • Because if they enter "6:00" without adding AM or PM, you don't know which one they mean. You should force them to specify one format or the other. – Blazemonger Oct 10 '13 at 20:32

1 Answers1

1

You can look into momentjs http://momentjs.com/ which is great for quick parsing of DateTimes. Problem is, if you allow input of both 24hr and 12hr slots, there is nothing that will stop somebody inputting a 12hr time in for a past-noon time unless you require AM or PM to be input as well.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
Mike H.
  • 1,731
  • 9
  • 31