Use JavaScript to convert a date string with timezone to a date object in local time(使用 JavaScript 将带有时区的日期字符串转换为本地时间的日期对象)
问题描述
我的日期字符串格式如下: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
我需要使用这些日期字符串创建一个日期对象.new Date()
不适用于此字符串.请帮我将这些日期字符串转换为具有本地时区的日期对象.
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/
以下应该解析您提供的日期,但您可能需要根据需要对其进行修改.
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 的字符串解析器,它看起来就像您提供的格式,除了时间的秒数和时区之间的空格.
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 将带有时区的日期字符串转换为本地时间的日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 JavaScript 将带有时区的日期字符串转换为本地时间的日期对象


- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01