如何確定日期字符串是否包含已在 Javascript 中添加的時間偏移量? (How to figure out if the Date string contains time offset added already in Javascript?)


問題描述

如何確定日期字符串是否包含已在 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_NinjaBergi)

參考文件

  1. How to figure out if the Date string contains time offset added already in Javascript? (CC BY‑SA 3.0/4.0)

#utc #javascript #Date #RegEx #datetimeoffset






相關問題

Java - 從外部服務器獲取 POSIX UTC 時間戳的最佳選擇? (Java - Best choice to get a POSIX UTC timestamp from external server?)

PHP - Параўнанне лакальнага і UTC часу са зрушэннем гадзіннага пояса (PHP - Local vs UTC Time Comparison with Timezone Offset)

以 UTC 格式轉換日期時間 (Convert date time in utc)

如何確定日期字符串是否包含已在 Javascript 中添加的時間偏移量? (How to figure out if the Date string contains time offset added already in Javascript?)

Delphi - Tải TTimeZone cho múi giờ không thuộc địa phương và chuyển đổi giữa các múi giờ (Delphi - Get TTimeZone for non-local timezone and convert between timezones)

什麼是“標準”時區縮寫? (What are the "standard" timezone abbreviations?)

如何從包含 Oracle 中時區偏移的日期/時間字符串中獲取 UTC 日期/時間 (How to get UTC date/time from a date/time string that contains timezone offset in Oracle)

如何在不使用 Javascript 的情況下在 ASP.Net 中呈現給定 UTC 日期時間值的本地時間? (How to render local time given UTC datetime values in ASP.Net without using Javascript?)

如何讓 Java 解析和格式化具有相同時區的日期/時間?我一直在獲取本地時區 (How do I get Java to parse and format a date/time with the same time zone? I keep getting the local timezone)

Python - 從 DST 調整的本地時間到 UTC (Python - From DST-adjusted local time to UTC)

7 位小數計算的 UTC 時間 (UTC time with 7 decimals calculation)

我們應用程序中的日期格式 (Date format in our Application)







留言討論