Date.parse failing in IE 11 with NaN

When i try to parse a date in IE 11, its throwing me NaN, but in chrome/firefox i get the below timestamp 1494559800000

Date.parse("?5?/?12?/?2017 09:00 AM")

Below is the condition which is failing for me in IE 11. Is there any other library or way i can fix this in IE 11.

tArray contains ["09:00 AM", "05:00 PM"];

var tArray = timings.toUpperCase().split('-');
var timeString1 = currentDate.toLocaleDateString() + " " + tArray[0];
var timeString2 = currentDate.toLocaleDateString() + " " + tArray[1];
var currentTimeString = currentDate.toLocaleDateString() + " " + currentTime.toUpperCase();
//Below is the condition which is failing.
if (Date.parse(timeString1) < Date.parse(currentTimeString) 
                 && Date.parse(currentTimeString) < Date.parse(timeString2)) {

I created a dummy fiddle where it fails.



According to MDN docs for Date.parse() parameter:


A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected).

Looks like Microsoft simply didn't implement the format you provided. I wouldn't use this format anyway, because it's locale dependent(might just be dd/mm/yyyy or sometimes might also fit mm/dd/yyyy).

An alternative to your solution is to use moment.js. It has a very powerful API for creating/parsing/manipulating dates. I'll show some examples on how you could use it:

//Create an instance with the current date and time
var now = moment();

//Parse the first the first argument using the format specified in the second
var specificTime = moment('5?/?12?/?2017 09:00 AM', 'DD/MM/YYYY hh:mm a');

//Compares the current date with the one specified
var beforeNow = specificTime.isBefore(now);

It offers a lot more and might help you simplify your code a great deal.

Edit: I rewrote your code using moment.js version 2.18.1 and it looks like this:

function parseDateCustom(date) {
    return moment(date, 'YYYY-MM-DD hh:mm a');

var tArray = ["09:00 AM", "05:00 PM"];
var currentDate = moment().format('YYYY-MM-DD') + ' ';
var timeString1 = parseDateCustom(currentDate + tArray[0]);
var timeString2 = parseDateCustom(currentDate + tArray[1]);
var currentTimeString = parseDateCustom(currentDate + "01:18 pm");

if (timeString1.isBefore(currentTimeString) && currentTimeString.isBefore(timeString2)) {
} else {


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.