0

I am programming a facebook chat bot and I receive the date and time from the user, I want to create an object of type Date().

Accepted formats from user:

Date:

15.9.2017
15/9/2017
15/09/2017
15.09.2017

Time:

15:30 et
15:30
3pm et
3pm

Would you recommend me some library (I've looked at moment.js)? How would you parse these formats?

EDIT

For Time validation I use this regex:

/((0?[0-9]{1,2} ?(pm|am) ?(et|at)?)|(0?[0-9]{1,2}:0?[0-9]{1,2} ?(pm) ?(et|at)?)|(0?[0-9]{1,2}:0?[0-9]{1,2} ?(et|at)?))/

For Date validation I use this regex:

/(0?[1-9]|[12][0-9]|3[01])[\/\-\.](0?[1-9]|1[012])[\/\-\.]\d{4}/
6
  • Use new Date(15/9/2017) in case of dot (.) replace dot with ( '/' or '-') Commented Jan 22, 2017 at 17:30
  • no I need the time included and all of the formats Commented Jan 22, 2017 at 17:31
  • Are those all the accepted formats? Would 2016-12-25 be rejected? Commented Jan 22, 2017 at 17:34
  • for now yes... I am slovak guy and I am programming for american people, what time do they use in America? I will paste the regex which is used for validation. Commented Jan 22, 2017 at 17:35
  • Well, the date notation in the US usually has the month come first MM/DD/YYYY. Different from Europe. Commented Jan 22, 2017 at 20:33

1 Answer 1

2

JavaScript has quite limited date/time support, and it is generally recommended to use a library for any parsing requirements.

In my opinion, the defacto DateTime library for JavaScript it Moment.js.

Using Moment.js, you can supply a date string like in your example, along with a format string which describes the format your date string is in.

For example:

var date = moment("15.9.2017", "D.M.YYYY");

For times you can use the H:m:s format string, and there is also a timezone extension for extra timezone manipulation.

All the docs are available at https://momentjs.com/docs/, the parsing section is here: https://momentjs.com/docs/#/parsing/

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

1 Comment

and what about time?

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.