我使用正则表达式从给定字符串中提取日期和时间信息。
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/