JavaScript 日期时间解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12825588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:35:07  来源:igfitidea点击:

JavaScript datetime parsing

javascriptparsingdatetimeformatdatetime-format

提问by vtokmak

Possible Duplicate:
How can I convert string to datetime with format specification in JavaScript?

可能的重复:
如何使用 JavaScript 中的格式规范将字符串转换为日期时间?

I have a json response which contains a hashmap like;

我有一个 json 响应,其中包含一个哈希图;

{"map":{"2012-10-10 03:47:00.0":23.400000000000002,"2012-10-10 03:52:00.0":23.3,"2012-10-10 03:57:00.0":23.3,"2012-10-10 04:02:00.0":23.3,"2012-10-10 04:07:00.0":23.200000000000003,"2012-10-10 04:13:00.0":23.1,"2012-10-10 04:18:00.0":23.1,"2012-10-10 04:23:00.0":23.0,"2012-10-10 04:28:00.0":23.0,"2012-10-10 04:33:00.0":23.0,"2012-10-10 04:38:00.0":22.900000000000002,"2012-10-10 04:43:00.0":22.8,"2012-10-10 04:48:00.0":22.8,"2012-10-10 04:53:00.0":22.700000000000003,"2012-10-10 04:58:00.0":22.6,"2012-10-10 05:03:00.0":22.6,"2012-10-10 05:08:00.0":22.5,"2012-10-10 05:13:00.0":22.5,"2012-10-10 05:18:00.0":22.5,"2012-10-10 05:23:00.0":22.400000000000002}}

I want to format datetime part of json like;

我想格式化json的日期时间部分;

dd/mm/yyyy HH:mm:ss

日/月/年 HH:mm:ss

Lets assume I put all pair elements like this;

让我们假设我把所有的元素都像这样;

var myArr = [["2012-10-10 03:47:00.0", 23.400000000000002], ["2012-10-10 03:52:00.0", 23.3], ....];

Then, I try to parse datetime part like below and I got Date {Invalid Date}on console;

然后,我尝试解析如下所示的日期时间部分,并在控制台上得到Date {Invalid Date}

new Date(myArr[0][0]);

How can I format this type of datetime.

如何格式化这种类型的日期时间。

回答by Chase

Try the following:

请尝试以下操作:

new Date(Date.parse(myArr[0][0]));

EXAMPLE

例子

Use the Date.parsemethod to parse the string into the number of milliseconds since January 1, 1970, 00:00:00 UTC. Take that number of milliseconds and call the Datemethod once again to turn that time into a date object.

使用日期parse方法将字符串解析为自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数。取该毫秒数并再次调用Date方法以将该时间转换为日期对象。

EDIT:

编辑

Well this may be a little ugly for this case, but it seems Firefox is having an issue with the -s and the 00.0.

好吧,对于这种情况,这可能有点难看,但 Firefox 似乎在-s 和00.0.

var myArr = [["2012-10-10 03:47:00.0", 23.400000000000002], ["2012-10-10 03:52:00.0", 23.3]];

var date = convertDateTime(myArr[0][0]);
console.log(date);

function convertDateTime(dateTime){
    dateTime = myArr[0][0].split(" ");

    var date = dateTime[0].split("-");
    var yyyy = date[0];
    var mm = date[1]-1;
    var dd = date[2];

    var time = dateTime[1].split(":");
    var h = time[0];
    var m = time[1];
    var s = parseInt(time[2]); //get rid of that 00.0;

    return new Date(yyyy,mm,dd,h,m,s);
}

EXAMPLE

例子

回答by jimbo

function dateFromString(str) {
  var m = str.match(/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)\.(\d+)/);
  return new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6] * 100);
}

dateFromString(myArr[0][0]); // Sat Oct 10 2012 03:47:00 GMT-0500 (EST)