在IE 11中使用NaN失败的Date.parse [英] Date.parse failing in IE 11 with NaN

查看:81
本文介绍了在IE 11中使用NaN失败的Date.parse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试解析 IE 11 中的日期时,它会把我扔给NaN,但是在chrome / firefox中我得到以下时间戳1494559800000

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")

以下是我在IE 11中失败的情况。是否还有其他库或方式我可以在IE 11中修复此问题。

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 包含 [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)) {

我创造了一个假小提琴,它失败了。
https://jsfiddle.net/vwwoa32y/

I created a dummy fiddle where it fails. https://jsfiddle.net/vwwoa32y/

推荐答案

根据 MDN 文档 Date.parse()参数:


dateString

dateString

表示RFC2822或ISO 8601日期的字符串(可以使用其他格式,但结果可能是意外的)。

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

看起来Microsoft根本没有实现您提供的格式。我不会使用这种格式,因为它依赖于区域设置(可能只是dd / mm / yyyy或者有时也可能适合mm / dd / yyyy)。

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

您的解决方案的替代方案是使用 moment.js 。它有一个非常强大的API,用于创建/解析/操作日期。我将展示一些如何使用它的示例:

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.

编辑:
我使用 moment.js 版本2.18.1重写了您的代码看起来像这样:

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

这篇关于在IE 11中使用NaN失败的Date.parse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆