簡體   English   中英

DateTimeFormat 是否考慮了夏令時?

[英]Does DateTimeFormat account for daylight savings time?

我正在將一些依賴於moment-timezone時間檢查轉換為使用INTL.DateTimeFormat

對於上下文,代碼以毫秒為單位( Date.now() )獲取用戶的當前時間,並針對三個時區(每個網站都有自己的語言環境)進行檢查:

const formatter = Intl.DateTimeFormat([], {
  timeZone: timezone,
  year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', 
  minute: 'numeric', second: 'numeric',
})

其中時區是America/New_YorkEurope/LondonEurope/Madrid Formatter 被傳遞到Date.parse以將其轉換為適當的ms格式。

我在確定該函數是否已經考慮了夏令時時遇到了一些麻煩。 如果沒有,我可以在其他地方做一些邏輯來轉換我將與適當的 DST 值進行比較的兩次。

這是已經存在的東西嗎? 它是在幕后,還是函數的第一個 arg 的一部分? 只是無法在文檔中找到信息。

嘗試這個:

 <html> <head> <script language="JavaScript"> // function to calculate local time // in a different city // given the city's UTC offset function calcTime(city, offset) { // create Date object for current location d = new Date(); // convert to msec // add local time zone offset // get UTC time in msec utc = d.getTime() + (d.getTimezoneOffset() * 60000); // create new Date object for different city // using supplied offset nd = new Date(utc + (3600000*offset)); // return time as a string return "The local time in " + city + " is " + nd.toLocaleString(); } alert(calcTime('New York ', '-4')); alert(calcTime('Madrid', '+1')); alert(calcTime('London', '+1')); </script> </head> <body> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM