簡體   English   中英

從特定時區獲取日期和時間

[英]Getting date and time from a specific timezone

我正在啟動我的新網站,該網站將於意大利時間上午9點啟動。 現在我給dateFuture = new Date(2013,0,30,9,00,00); 但是需要我在印度的本地PC日期。 我如何在意大利的上午9點顯示該網站?

要獲得意大利當地時間上午9點的印度當地時間,請執行以下操作:

var offset = new Date().getTimezoneOffset() / 60 ;
var d = new Date(Date.UTC(2013,0,30,9 + offset,0,0));

我了解您要防止在特定時間段之前看到某個頁面。 如您所知,JavaScript在瀏覽器中運行,僅憑時間就無法確定用戶是在意大利還是在其他地方打開頁面。

您可以選擇的最接近的時間是確保在該時區的上午9點之后。

var now = new Date(); 
var nowUtc = new Date(
    now.getUTCFullYear(), 
    now.getUTCMonth(), 
    now.getUTCDate(),         
    now.getUTCHours(),
    now.getUTCMinutes(), 
    now.getUTCSeconds());

var eightAmUTC = new Date(2013,0,30,8,00,00); // 8am UTC is 9am In Italy for this date.

var nowIsAfter9InItaly = nowUtc > eightAmUTC;

如果1月30日上午9點在意大利,nowIsAfter9InItaly將為true ,否則為false

暫無
暫無

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

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