7

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. https://jsfiddle.net/vwwoa32y/

Shane
  • 5,517
  • 15
  • 49
  • 79
  • 1
    The string you're showing contains a bunch of unprintable characters (`Date.parse("<00e2><0080><008e>5...`), are you sure it's not caused by that? – robertklep May 12 '17 at 18:06
  • @robertklep: where am i showing that? – Shane May 12 '17 at 18:09
  • Well, they are unprintable, so they're not _showing_, but they are _there_. Cutting and pasting that line of code into either Node or the Chrome console and executing it returns `NaN` in both cases. – robertklep May 12 '17 at 18:10
  • @robertklep: you mean this line, Date.parse("‎5‎/‎12‎/‎2017 09:00 AM"), that i was trying... – Shane May 12 '17 at 18:11
  • Yes, that line. The characters are [`‎`](https://en.wikipedia.org/wiki/Left-to-right_mark). – robertklep May 12 '17 at 18:11
  • `Date.parse()` is only guaranteed to parse a subset of ISO 8601 date formats as defined in the JavaScript spec, namely `yyyy-mm-ddThh:mm:ss.sssZ`, or an offset instead of `Z`. If any browser successfully parses any other format, you are just lucky. Use the moment library for generalized date parsing and manipulation. –  May 12 '17 at 18:14
  • @robertklep: that i copy pased from debugger, but the actual code above fails... – Shane May 12 '17 at 18:14
  • In that case I defer to @torazaburo's comment :) – robertklep May 12 '17 at 18:15
  • The fiddle in IE 11 fails... https://jsfiddle.net/vwwoa32y/ – Shane May 12 '17 at 18:23

1 Answers1

5

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

dateString

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)) {
    console.log('Sucess');
} else {
    console.log('Failed');
}
Riiverside
  • 788
  • 7
  • 12