I am having trouble getting valid date formats using moment.js. I am not able to reject the entries which have only date and not time.
The date should be a "month-day-year" formatted (US style) and should include time in hours:min.
Examples of allowed formats:
- 2/02/2019 0:00
- 31/12/2019 0:00
- 31/2/2019 00:00
Here is my code:
moment(item["InputDate"], 'MM/DD/YYYY HH:mm', true).isValid()
If I try to use strict formatting option, I am not able to validate the dates where there is a single digit date or month. I am taking this input from string type through excel input.
Is there a way to just check if the date is in the format mentioned and not having time?
Edit 1: I ended up listing out the valid formats and added them to strict validation using moment.js validation as a temporary workaround. But I am not sure if this is an exhaustive list of formats. I am using the below as the list of valid formats:
- "MM/DD/YYYY HH:mm"
- "MM/D/YYYY HH:mm"
- "M/DD/YYYY HH:mm"
- "MM/DD/YY HH:mm"
- "MM/DD/YYYY H:mm"
- "M/D/YYYY HH:mm"
- "M/DD/YY HH:mm"
- "M/DD/YYYY H:mm"
- "M/D/YY HH:mm"
- "M/D/YY H:mm"
- "MM/D/YY HH:mm"