使用 JavaScript 将带时区的日期字符串转换为本地时间的日期对象

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

Use JavaScript to convert a date string with timezone to a date object in local time

javascriptdatetimetimezone

提问by Marina

The format of my date string looks like this: yyyy-MM-ddTHH:mm:ss-0Z00

我的日期字符串的格式如下所示: yyyy-MM-ddTHH:mm:ss-0Z00

Example 1: 2010-03-05T07:03:51-0800

示例 12010-03-05T07:03:51-0800

Example 2: 2010-07-01T20:23:00-0700

示例 22010-07-01T20:23:00-0700

I need to create a date object using these date strings. new Date()does not work on this string. Please help me convert these date strings into a date objects with the local timezone.

我需要使用这些日期字符串创建一个日期对象。new Date()不适用于此字符串。请帮我将这些日期字符串转换为具有本地时区的日期对象。

Thank you!

谢谢!

Edit: I am using this in Pentaho Data Integration 4.3.0.

编辑:我在 Pentaho Data Integration 4.3.0 中使用它。

采纳答案by Andrew M

You can use a library such as Moment.jsto do this.

您可以使用诸如Moment.js 之类的库来执行此操作。

See the String + Format parsing.

请参阅字符串 + 格式解析。

http://momentjs.com/docs/#/parsing/string-format/

http://momentjs.com/docs/#/parsing/string-format/

The following shouldparse your date you provided, but you may need to modify it for your needs.

以下内容解析您提供的日期,但您可能需要根据需要对其进行修改。

var oldDate = "2010-03-05T07:03:51-0800";

var dateObj = moment(oldDate, "YYY-MM-DDTHH:mm:ssZ").toDate();

Alternatively, see Moment's String parser, which looks like it is in the format you provided, with the exception of a space between the seconds of the time and the time zone.

或者,请参阅 Moment 的字符串解析器,它看起来像您提供的格式,但时间的秒数和时区之间有一个空格。

http://momentjs.com/docs/#/parsing/string/

http://momentjs.com/docs/#/parsing/string/

Alternative

选择

A second way of doing this is Date.js, another library that seems to parse the format just fine. http://www.datejs.com

第二种方法是 Date.js,另一个似乎可以很好地解析格式的库。http://www.datejs.com

回答by Leo

Take my timezone as an example (AEST):

以我的时区为例(AEST):

function parseDate(str_date) {
  return new Date(Date.parse(str_date));
}


var str_date = "2015-05-01T22:00:00+10:00"; //AEST time
var locale_date = parseDate(str_date);

locale_date: Fri May 01 2015 22:00:00 GMT+1000 (AEST)

locale_date:2015 年 5 月 1 日星期五 22:00:00 GMT+1000 (AEST)

var str_date = "2015-05-01T22:00:00+00:00" //UTC time
var locale_date = parseDate(str_date);

locale_date: Sat May 02 2015 08:00:00 GMT+1000 (AEST)

locale_date:2015 年 5 月 2 日星期六 08:00:00 GMT+1000 (AEST)

回答by Joe Johnson

Date String:

日期字符串:

var strDate = "2010-07-01T20:23:00-0700";

var strDate = "2010-07-01T20:23:00-0700";

To local time representation in native JS Date object:

在原生 JS Date 对象中的本地时间表示:

var ltzDate = (new Date(strDate)).toLocaleString();

var ltzDate = (new Date(strDate)).toLocaleString();