JavaScript Date.parse返回Mozilla浏览器中的NaN [英] JavaScript Date.parse return NaN in Mozilla Browser
问题描述
Mozilla浏览器我试过在JavaScript中像 strtotime
在PHP中获取时间戳
我的代码:
// var start_date = data.result [0] .start_date;
var start_date =2011-01-26 13:51:50;
var d = Date.parse(start_date)/ 1000;
console.log(d);
// 1296030110
以上代码在chrome中正常工作。但不能在Mozilla浏览器中工作。我正在获得 NaN
值。请帮助我。
在google搜索后,我找到一个解决方案,在日期和时间之间添加 T
。所以我补充说。我得到输出,但两个浏览器的输出不一样。
var start_date =2011-01-26T13: 51:50
var d = Date.parse(start_date)/ 1000;
console.log(d);
// Mozilla = 1296030110
// Chrome = 1296044910
function parseDateTime(s){var b = s.split(/ \D /);返回新日期(b [0],b [1] -1,b [2],b [3],b [4],b [5])} document.write(parseDateTime(2011-01-26 13 :51:50)/ 1000);
对缺失值的支持会再增加一个代码。
Mozilla browser I have tried get my time-stamp in JavaScript like strtotime
in php
My Code:
//var start_date = data.result[0].start_date;
var start_date = "2011-01-26 13:51:50";
var d = Date.parse(start_date) / 1000;
console.log(d);
// 1296030110
Above code is working fine in chrome. But not working in the Mozilla Browser. I am getting NaN
value. Please help me.
After search in google I find one solution to add T
between the date and time. so I have added. I am getting the output but the output is not the same in both browser.
var start_date = "2011-01-26T13:51:50";
var d = Date.parse(start_date) / 1000;
console.log(d);
//Mozilla = 1296030110
//Chrome = 1296044910
Do not parse strings with the Date constructor or Date.parse (they do the same thing), it is extremely unreliable, especially for non–standard strings (and some that are). To parse "2011-01-26 13:51:50" as a local time, use a library or a simple function like:
function parseDateTime(s) {
var b = s.split(/\D/);
return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5])
}
document.write(parseDateTime("2011-01-26 13:51:50") / 1000);
To include validation an support for missing values adds a bit more code on one more line.
这篇关于JavaScript Date.parse返回Mozilla浏览器中的NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!