問題描述
如何確定日期字符串是否包含已在 Javascript 中添加的時間偏移量? (How to figure out if the Date string contains time offset added already in Javascript?)
Consider this date string:
"2012‑08‑20T15:00:00‑07:00"
It has time offset added as "‑07:00"
How can I find out it has this offset added? I know I can do it with regular expressions. But is there any other easier way?
Or does any one have the regex for it?
‑‑‑‑‑
參考解法
方法 1:
I know I can do it with regular expressions, but is there any other easier way?
I don't think so, regexes are the simplest pattern matchers available.
does any one have the regex for it?
I think this should do it:
/[+‑]\d\d:\d\d$/.test(datestring)
If you want to parse it, use
var match = /([+‑]?\d\d):(\d\d)$/.exec(datestring);
if (match)
return parseInt(match[1], 10)*60 + parseInt(match[2], 10)*(match[1].charAt(0)=="‑"?‑1:1);
return 0;
(by Cute_Ninja、Bergi)