admin管理员组

文章数量:1414613

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. /

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/vwwoa32y/

Share Improve this question edited May 12, 2017 at 18:20 Shane asked May 12, 2017 at 17:58 ShaneShane 5,69715 gold badges57 silver badges82 bronze badges 9
  • 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 Commented May 12, 2017 at 18:06
  • @robertklep: where am i showing that? – Shane Commented May 12, 2017 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 Commented May 12, 2017 at 18:10
  • @robertklep: you mean this line, Date.parse("‎5‎/‎12‎/‎2017 09:00 AM"), that i was trying... – Shane Commented May 12, 2017 at 18:11
  • Yes, that line. The characters are &lrm;. – robertklep Commented May 12, 2017 at 18:11
 |  Show 4 more ments

1 Answer 1

Reset to default 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');
}

本文标签: javascriptDateparse failing in IE 11 with NaNStack Overflow