日期无效,但日期无效 [英] Invalid Date Issue but Date is Invalid

查看:215
本文介绍了日期无效,但日期无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期格式


03.03.2016 20:01

03.03.2016 20:01

我有一个代码

aData._date = new Date(aData[3]).getTime();

错误是该日期无效,但在不同的计算机上它运行良好,但现在它不工作。有什么问题?

error is that date is invalid but, at different computer it worked well but now it is not working .What is the problem?

提前感谢

推荐答案


有什么问题?

What is the problem?

使用Date构造函数分析字符串(或Date.parse,它们等价于解析)在很大程度上取决于实现,不推荐。手动解析字符串,如果您只需处理单一格式,或使用库(有很多好的可选择),或者提供其他格式的小功能。

Parsing of strings using the Date constructor (or Date.parse, they are equivalent for parsing) is largely implementation dependent and is not recommended. Manually parse strings, either with a small function if you only have to deal with a single format, or use a library (there are many good ones to choose from) and provide the format otherwise.

ECMAScript 2015指定 Date.parse 正确解析ISO 8601扩展格式日期,但任何其他格式都取决于实现。许多正在使用的浏览器不正确(即根据规范)分析ISO 8601格式日期。

ECMAScript 2015 specifies that Date.parse correctly parse ISO 8601 extended format dates, however any other format is implementation dependent. Many browsers in use do not correctly (i.e. per the specification) parse ISO 8601 format dates either.

03.03.2016 20:01不是ISO 8601日期格式。假设它是DD.MM.YYYY hh:mm,可以使用以下方式将其解析为本地日期和时间:

"03.03.2016 20:01" is not an ISO 8601 date format. Assuming it's DD.MM.YYYY hh:mm it can be parsed as a local date and time using:

function parseDMYHM(s){
  var b = ('' || s).split(/\D/);
  return new Date(b[2], b[1]-1, b[0], b[3], b[4]);
}

document.write(parseDMYHM('03.03.2016 20:01'));

或者如果您有一个库, / em>函数接受一个格式来解析(像这样的库,这是任何好的意思),使用如下:

Or if you have a library with a parse function that accepts a format to parse (as such libraries that are any good will), using something like:

var d = parse('03.03.2016 20:01', 'DD.MM.YYYY hh:mm');

这篇关于日期无效,但日期无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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