使用`new Date`解析日期字符串时出错 [英] Error parsing date string with `new Date`
问题描述
datefromJSON = req.body.occasion_date;
occasion_date = new Date(datefromJSON);
console.log(occasion_date);
//while running this i get log like this
//"Invalid Date"
当我从req.body获取json数据然后保存在datefromJSON var中,并且存储到数据库中我用来转换为Date对象获取错误
when i get json data from req.body then save in datefromJSON var, and for storing into database i use to convert into Date object that get error
推荐答案
您的日期字符串(无论是 31-08-2016
)不是以 Date
构造函数识别的格式。所以你最终得到了一个 Date
,其基础时间值是 NaN
,当你显示为无效日期时请求字符串版本。无条件的例子:
Your date string (whatever it is "31-08-2016"
) isn't in a format recognized by the Date
constructor. So you ended up with a Date
whose underlying time value is NaN
, which is shown as "Invalid Date" when you ask for the string version. Gratuitous example:
console.log(new Date("foobar").toString());
仅格式规范要求支持的JavaScript实现是在2009年的ES5规范中添加的,实际上是(并且ES2015实际上是ES5中的错误)ISO-8601的子集。例如:
The only format the specification requires a JavaScript implementation to support is the one added in the ES5 specification in 2009, which was meant to be (and as of ES2015 actually is; there was an error in ES5) a subset of ISO-8601. So for instance:
console.log(new Date("2016-08-31T09:25").toString());
每一个我已经遇到的JavaScript实现非正式支持使用斜杠解析US格式, MM / dd / yyyy
,(即使在非美国语言环境中) ),但时区各不相同(大部分都将其解释为当地时间,其他时间则按GMT解释)。
Every JavaScript implementation I've run into also unofficially supports parsing the U.S. format with slashes, MM/dd/yyyy
, (even in non-U.S. locales), but the timezone varies (most interpret it as local time, others interpret it in GMT).
所以你需要:
-
解析你的字符串(正则表达式,
split
等)并使用表格日期
支持单独提供部件的构造函数。请注意月份值从0 = 1月开始(不是1 = 1月)。有几十个问题,这里有关于SO的例子。以下是将常见的'dd-MM-yyyy HH:mm:ss或'dd-MM-yyyy HH:mm:ss.SSS
格式解析为本地日期的示例/时间:
Parse your string (regular expressions,
split
, etc.) and use the form of theDate
constructor that supports supplying the parts individually. Mind the gotcha that the months value starts with 0 = January (not 1 = January). There are several dozen questions with examples of that here on SO. Here's an example of parsing the common 'dd-MM-yyyy HH:mm:ssor 'dd-MM-yyyy HH:mm:ss.SSS
format as a local date/time:
基于正则表达式:
function parseIt(str) {
var rex = /^\s*(\d{1,2})[\/-](\d{1,2})[\/-](\d{4})(?: (\d{1,2})(?::(\d{1,2})(?::(\d{1,2})(?:\.(\d{1,3}))?)?)?)?\s*$/;
var parts = rex.exec(str);
var dt = null;
if (parts) {
dt = new Date(+parts[3], // Year
+parts[2] - 1, // Month
+parts[1], // Day
+parts[4] || 0, // Hours
+parts[5] || 0, // Minutes
+parts[6] || 0, // Seconds
+parts[7] || 0 // Milliseconds
);
}
return dt;
}
function test(str) {
var dt = parseIt(str);
console.log(str, "=>");
console.log(" " + String(dt));
console.log(" (" + (dt ? dt.toISOString() : "null") + ")");
}
test("foobar");
test("31-08-2016");
test("31/08/2016");
test("31/08/2016 9");
test("31/08/2016 9:25");
test("31/08/2016 09:25");
test("31/08/2016 09:25:17");
test("31/08/2016 09:25:17.342");
正则表达式看起来很复杂,但它实际上只是嵌套在非捕获,可选组内的一堆捕获组。 此处的解释。
- 使用库(如 MomentJS ),可以通过说明格式是什么来解析字符串。
- Use a library (like MomentJS) that lets you parse a string by saying what the format is.
这篇关于使用`new Date`解析日期字符串时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!