案例一:
new Date(Date.parse("Jul 8, 2005"));
输出:
2005年7月08日上午00:00 GMT-0700 (PST)
案例二:
new Date(Date.parse("2005-07-08"));
输出:
2005 年 7 月 7 日星期四 17:00:00 GMT-0700 (PST)
为什么第二个解析不正确?
案例一:
new Date(Date.parse("Jul 8, 2005"));
输出:
2005年7月08日上午00:00 GMT-0700 (PST)
案例二:
new Date(Date.parse("2005-07-08"));
输出:
2005 年 7 月 7 日星期四 17:00:00 GMT-0700 (PST)
为什么第二个解析不正确?
当前回答
CMS接受的答案是正确的,我刚刚添加了一些功能:
修剪和清洁输入空间 解析斜杠、破折号、冒号和空格 有默认的日期和时间
// parse a date time that can contains spaces, dashes, slashes, colons
function parseDate(input) {
// trimes and remove multiple spaces and split by expected characters
var parts = input.trim().replace(/ +(?= )/g,'').split(/[\s-\/:]/)
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2] || 1, parts[3] || 0, parts[4] || 0, parts[5] || 0); // Note: months are 0-based
}
其他回答
另一种解决方案是构建具有日期格式的关联数组,然后重新格式化数据。
此方法对于以不寻常的方式格式化的日期非常有用。
一个例子:
mydate='01.02.12 10:20:43':
myformat='dd/mm/yy HH:MM:ss';
dtsplit=mydate.split(/[\/ .:]/);
dfsplit=myformat.split(/[\/ .:]/);
// creates assoc array for date
df = new Array();
for(dc=0;dc<6;dc++) {
df[dfsplit[dc]]=dtsplit[dc];
}
// uses assc array for standard mysql format
dstring[r] = '20'+df['yy']+'-'+df['mm']+'-'+df['dd'];
dstring[r] += ' '+df['HH']+':'+df['MM']+':'+df['ss'];
CMS接受的答案是正确的,我刚刚添加了一些功能:
修剪和清洁输入空间 解析斜杠、破折号、冒号和空格 有默认的日期和时间
// parse a date time that can contains spaces, dashes, slashes, colons
function parseDate(input) {
// trimes and remove multiple spaces and split by expected characters
var parts = input.trim().replace(/ +(?= )/g,'').split(/[\s-\/:]/)
// new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2] || 1, parts[3] || 0, parts[4] || 0, parts[5] || 0); // Note: months are 0-based
}
使用moment.js来解析日期:
var caseOne = moment("Jul 8, 2005", "MMM D, YYYY", true).toDate();
var caseTwo = moment("2005-07-08", "YYYY-MM-DD", true).toDate();
第三个参数决定严格解析(从2.3.0开始可用)。没有它moment.js也可能会给出不正确的结果。
According to http://blog.dygraphs.com/2012/03/javascript-and-dates-what-mess.html the format "yyyy/mm/dd" solves the usual problems. He says: "Stick to "YYYY/MM/DD" for your date strings whenever possible. It's universally supported and unambiguous. With this format, all times are local." I've set tests: http://jsfiddle.net/jlanus/ND2Qg/432/ This format: + avoids the day and month order ambiguity by using y m d ordering and a 4-digit year + avoids the UTC vs. local issue not complying with ISO format by using slashes + danvk, the dygraphs guy, says that this format is good in all browsers.
虽然CMS是正确的,将字符串传递到解析方法通常是不安全的,但新的ECMA-262第5版(又名ES5)规范在15.9.4.2节中建议Date.parse()实际上应该处理iso格式的日期。旧的规范没有这样的要求。当然,旧的浏览器和一些当前的浏览器仍然不提供这种ES5功能。
你的第二个例子没有错。正如date .prototype. toisostring()所暗示的那样,它是以UTC为单位指定的日期,但以本地时区表示。