javascript - 使用正则表达式,Javascript 中是否还有其他方法可以从具有时区的字符串中提取日期

标签 javascript regex string datetime

我使用正则表达式从给定字符串中提取日期和时间信息。

2006-08-15T00:00:00+05:30

我是正则表达式的新手,我的做法如下:

(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})

我知道可能有更好的方法来做到这一点。因此,如果有人对此主题有任何了解,请分享和解释。 P.S:我还想提取时区信息。

最佳答案

如果你想要一个非正则表达式的解决方案,你可以使用这个:

> new Date(Date.parse("2005-07-08T11:22:33+0000"))
Fri Jul 08 2005 13:22:33 GMT+0200 (CEST)
> new Date(Date.parse("2005-07-08T11:22:33+0000")).toUTCString()
"Fri, 08 Jul 2005 11:22:33 GMT"

要获取时区,您可以使用getTimezoneOffset()函数

var my_date = new Date(Date.parse("2005-07-08T11:22:33+0000"));
var timezone_offset = my_date.getTimezoneOffset();

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight savings time prevents this value from being a constant even for a given locale

关于javascript - 使用正则表达式,Javascript 中是否还有其他方法可以从具有时区的字符串中提取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542379/

相关文章:

javascript - 插入事务后如何保存一对多关系

使用 Notepad++ 查找双括号的正则表达式

regex - 文本消息与数千个正则表达式的高效匹配

javascript - 正则表达式获取每个单词的中间部分javascript

python - 在字符上拆分,但保留该字符 - python

javascript - 一次选择多个数组元素

javascript - 正则表达式:将文本拆分为单词并保留换行符

java - 为什么 String.split 需要转义管道分隔符?

c# - Visual Studio 2013 : Testing/Assert Strings

javascript - 使用两个选择器选择一个元素