問題描述
我的日期字符串格式如下:yyyy-MM-ddTHH:mm:ss-0Z00
The format of my date string looks like this: yyyy-MM-ddTHH:mm:ss-0Z00
示例 1:2010-03-05T07:03:51-0800
示例 2:2010-07-01T20:23:00-0700
我需要使用這些日期字符串創(chuàng)建一個日期對象.new Date()
不適用于此字符串.請幫我將這些日期字符串轉(zhuǎn)換為具有本地時區(qū)的日期對象.
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.
謝謝!
我在 Pentaho Data Integration 4.3.0 中使用它.
I am using this in Pentaho Data Integration 4.3.0.
推薦答案
可以使用Moment.js等庫這樣做.
查看字符串+格式解析.
See the String + Format parsing.
http://momentjs.com/docs/#/parsing/string-format/
以下應該解析您提供的日期,但您可能需要根據(jù)需要對其進行修改.
The following should parse 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();
或者,請參閱 Moment 的字符串解析器,它看起來就像您提供的格式,除了時間的秒數(shù)和時區(qū)之間的空格.
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.
http://momentjs.com/docs/#/parsing/string/
第二種方法是 Date.js,另一個似乎可以很好地解析格式的庫.http://www.datejs.com
A second way of doing this is Date.js, another library that seems to parse the format just fine. http://www.datejs.com
這篇關于使用 JavaScript 將帶有時區(qū)的日期字符串轉(zhuǎn)換為本地時間的日期對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!