2

Have a text input box.
Would like the user to enter a string in the exact format:

2010/08/26 17:04:51.24  

(with any numbers of course)

How can I do this efficiently?

Not looking for the answer, just a point in the right direction.

Thank You.

(html, JavaScript and jQuery)

2 Answers 2

2

Regular expressions would be the most obvious solution. That being said, you probably don't want to go through the trouble of implementing a regular expression for matching dates and times when you can instead use an already existing solution.

DateJs is a really great JavaScript date parser that handles converting user-entered dates and times into meaningful values. DateJs can intelligently parse natural language expressions like "tomorrow" or "+5 days" and also standard date and time representations like "10/24/2008 11:31:04 AM".

Sign up to request clarification or add additional context in comments.

2 Comments

DateJs looks good but I put my string in and it didn't know what to do! I think regular expressions are the way to go.
I think it's the ".24" at the end that it doesn't know how to handle. Is that significant?
1

I believe you'd want a regular expression to do this. I don't know of any other way to validate format other than that. I'm sure there are some libraries/plugins, but they probably just use a regular expression inside of them.

Edit: Nathan Taylor is right, DateJS is pretty great.

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.