使用 JavaScript 解析时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1856199/
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-22 21:38:10  来源:igfitidea点击:

Use JavaScript to Parse Time

javascriptdatetime

提问by Dodinas

This is probably something easy, but I'm a bit confused how to do this. How can I, using JavaScript, parse only the time from the following ISO 8601date string:

这可能很容易,但我有点困惑如何做到这一点。我如何使用 JavaScript 仅解析以下ISO 8601日期字符串中的时间:

 2009-12-06T17:10:00

In other words, with the string above, I'd like to output:

换句话说,对于上面的字符串,我想输出:

5:10 PM

Any guidance/tutorials on this would be great.

任何关于这方面的指导/教程都会很棒。

回答by Maksym Kozlenko

Chrome & Firefox: Standard JavaScript Date constructor takes ISO 8601 date string. For example:

Chrome 和 Firefox:标准 JavaScript 日期构造函数采用 ISO 8601 日期字符串。例如:

var sampleDate = new Date("2010-03-07T02:13:46Z");

var sampleDate = new Date("2010-03-07T02:13:46Z");

Returns this Date object: "Sun Mar 07 2010 13:13:46 GMT+1100 (AUS Eastern Daylight Time)"

返回此日期对象:“Sun Mar 07 2010 13:13:46 GMT+1100(澳大利亚东部夏令时间)”

This does not work in IE (including latest IE 9)

这在 IE 中不起作用(包括最新的 IE 9)

Here is a cross-browser solution by Paul Sowden at http://delete.me.uk/2005/03/iso8601.html:

这是 Paul Sowden 在http://delete.me.uk/2005/03/iso8601.html 上的跨浏览器解决方案:

Date.prototype.setISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    this.setTime(Number(time));
}

Usage:

用法:

var date = new Date();
date.setISO8601("2005-03-26T19:51:34Z");

If you do a lot of datetime manipulation in JavaScript, I can also suggest to check some JS libraries like MomentJS. It handles some common things like date parsing, formatting and calculating difference between two dates and has support for multiple localizations.

如果你在 JavaScript 中做了很多日期时间操作,我也可以建议检查一些 JS 库,比如MomentJS。它处理一些常见的事情,如日期解析、格式化和计算两个日期之间的差异,并支持多种本地化。

回答by me_and

string.slice(11,16)will return 17:10. From there (possibly using slicein more interesting and exciting manners) it should be fairly simple to get it into 24-hour format.

string.slice(11,16)会回来17:10。从那里(可能slice以更有趣和令人兴奋的方式使用)将其转换为 24 小时格式应该相当简单。

回答by ZZ Coder

This is so called ISO 8601 format. Mochikit comes with a function to parse it,

这就是所谓的 ISO 8601 格式。Mochikit 附带了一个解析它的函数,

http://mochikit.com/doc/html/MochiKit/DateTime.html

http://mochikit.com/doc/html/MochiKit/DateTime.html

You can get a Date object like this,

你可以得到一个像这样的 Date 对象,

  timestamp = isoTimestamp("2009-12-06T17:10:00");

Just copy the function if you don't want use Mochikit.

如果您不想使用 Mochikit,只需复制该功能。

回答by peller

Parsing the ISO timestamp is easy, formatting the time in a culturally appropriate way is hard (5:10 PM is not appropriate for all locales) Many toolkits provide routines for the ISO part, and it's even part of the new ECMAScript 5 standard; only a couple do the latter part, however.

解析 ISO 时间戳很容易,以文化上适当的方式格式化时间很难(下午 5:10 不适用于所有语言环境)许多工具包为 ISO 部分提供例程,它甚至是新的 ECMAScript 5 标准的一部分;然而,只有少数人会做后半部分。

You can try dojo.date.stamp.fromISOStringand dojo.date.locale.format.

您可以尝试dojo.date.stamp.fromISOStringdojo.date.locale.format

I believe Date-JS can format times also.

我相信 Date-JS 也可以格式化时间。