0

我旅行了很多,我經常有約會(如航班),在一個時區開始並以另一個時區結束。我需要知道每個事件將開始和結束的本地時間和UTC時間。Google腳本/ JavaScript在時區之間轉換時間

我使用Google日曆,並在日曆事件中指定不同的開始和結束時區。

我已經寫了一個腳本,拉我的議程給定的時間段,這顯示了我在當地時間的一切,這很好。

我很難得到當地時間。

當我檢索事件時,時間顯示在我的當地時間。

我可以檢索歐洲/羅馬等格式的時區,但這並不能幫助我顯示當地時間。

附件是我的日曆條目的截圖:

Google Calendar Screenshot

這裏是如何的事件從我的腳本(從日誌文件中獲取)看到:

Start: 2017-03-29T16:35:00+01:00, startTimeZone: Europe/Berlin. 
End: 2017-03-29T22:30:00+01:00, endTimeZone: Asia/Qatar 

這些時間應該分別是開始:17:35和結束:00:30。

有沒有人有任何想法如何獲得本地時間或時區之間轉換?

+0

顯示腳本的幾行內容。腳本中的任何日期代碼都使用腳本的時區作爲默認值。您的腳本擁有與日曆時區不同的自己的時區。默認情況下,時區是相同的,除非你改變它們。你在哪裏顯示你的腳本的時間?在對話框或側邊欄中?在電子表格中? –

+0

嗨桑迪,謝謝你的回覆。我不認爲我的問題很好。我的日期格式爲「2017-03-30T01:01:00 + 01:00」,時區爲「亞洲/卡塔爾」。我想在電子表格中顯示時間爲'03:01',這是約會發生時在卡塔爾的時間。從我的腳本返回的時間是在我的日曆的當地時間,這很好。我需要根據事件發生的時區對其進行調整。夏令時開始日期和結束日期和規則不盡相同,這很難實現,所以我非常想要一種方式來說明給定日期的「亞洲/卡塔爾」是GMT +/- X小時。 – kxm

+0

我會先看看'Utilities.formatDate()'的文檔,這可能會自動處理日光節約問題,所以你不需要自己編寫它。 [鏈接到Apps腳本文檔](https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)) –

回答

1

希望這可以幫助你。

function timezones() 
{ 
    var today = new Date(); 
    var hour = 3600000; 
    var tz = ''; 
    today = new Date(today.getTime() + (13 * hour)); 
    var s = '<div id="mydiv">'; 
    for(var i = 12; i >= -11; i--) 
    { 
    if(i > 0){ tz = 'GMT+' + i;} 
    if(i == 0){tz = 'GMT';} 
    if(i < 0){tz = 'GMT' + i;} 
    today = new Date(today.getTime() - hour); 
    s += '<div class="myDates">' + Utilities.formatDate(today,"GMT", "'Date:'yyyy-MM-dd' Time:'HH:mm:ss") + 'TimeZone; "' + tz + '"</div>'; 
    } 
    s += '</div>'; 
    dispStatus('Timezones', s); 
} 

這是我使用的顯示程序。

function dispStatus(title,html,width,height,modal) 
{ 
    var title = typeof(title) !== 'undefined' ? title : 'No Title Provided'; 
    var width = typeof(width) !== 'undefined' ? width : 400; 
    var height = typeof(height) !== 'undefined' ? height : 300; 
    var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>'; 
    var modal = typeof(modal) !== 'undefined' ? modal : false; 
    var htmlOutput = HtmlService 
    .createHtmlOutput(html) 
    .setWidth(width) 
    .setHeight(height); 
if(!modal) 
{ 
    SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title); 
} 
else 
{ 
    SpreadsheetApp.getUi().showModalDialog(htmlOutput, title); 
} 
} 

And here是一個時區地圖。

+0

Hi Cooper。感謝您的回覆。我不認爲我的問題很好。基本上,我在本地日曆的時區中找回日期和時間。實際的約會在另一個時區,並且有一個timezone屬性來告訴我它所在的時區。我想將本地時間添加到我的電子表格中,例如,我在'亞洲/卡塔爾'時區收到'22:30',我想表明這是'00:30',也就是約會發生時在卡塔爾的時間。 – kxm

+1

實質上,您需要爲東經15度經度添加一小時,並在西經15度經度減一小時。仔細看看這個[地圖](https://www.timeanddate.com/time/map/)它會講述整個故事。 – Cooper

+0

謝謝,但有時區「亞洲/卡塔爾」並沒有給我經/多的時間,所以我無法計算出時區的東或西有多少度數。它也不涉及夏令時,因國家而不同,有時甚至在國家內也不相同。我需要這個工作以適應日曆中的不斷變化,所以我需要一種動態計算本地時間的編程方式:/ – kxm

0
<h1>Calendar Play</h1> 
function calendarTesting() 
    { 

    var rngA = CalendarApp.getAllOwnedCalendars(); 
    var s=''; 
    for(var i = 0;i < rngA.length; i++) 
    { 
    s += '<div id="Cal' + i + '"><Strong>Name: </strong>' + rngA[i].getName(); 
    s += '<br /><strong>Color: </strong><span style="color:'+ 'black' +';">' + rngA[i].getColor() + '</span>'; 
    s += '<br /><strong>Description: </strong>' + rngA[i].getDescription(); 
    var pri = (rngA[i].isMyPrimaryCalendar())? 'Yes': 'No'; 
    s += '<br /><strong>IsMyPrimaryCalender: </strong>' + pri + '</div>'; 
    s += '<style>#Cal'+ i +'{background-color:'+ rngA[i].getColor() +';padding:10px;}</style>'; 

    } 
    var now = new Date(); 
    var ForWeeksFromNow = new Date(now.getTime() + (28 * 24 * 60 * 60 * 1000)); 
    var events = CalendarApp.getDefaultCalendar().getEvents(now,ForWeeksFromNow); 
    for(var j = 0; j < events.length; j++) 
    { 
    s+= '<div style="border:1px solid black;padding:10px 50px 10px 10px;">'; 
    s += '<div style="float:right;font-size:48px;">' + Utilities.formatDate(events[j].getStartTime(),"GMT-7", "E") + '</div>'; 
    s += '<br /><strong>Title: </strong>' + events[j].getTitle(); 
    s += '<br /><strong>Start: </strong>' + Utilities.formatDate(events[j].getStartTime(),"GMT-7", "MMM dd HH:mm"); 
    s += '<br /><strong>End: </strong>' + Utilities.formatDate(events[j].getEndTime(), "GMT-7", "MMM dd HH:mm"); 
    s += '<div style="float:right;font-size:12px;">' + events[j].getDescription() + '</div>'; 
    s += '</div>'; 

    } 
    s += '<br /><br /><input type="button" value="Exit" onClick="google.script.host.close();" />'; 
    var title = 'Calendar for: ' + Utilities.formatDate(now, "GMT-7", "E MMM dd, yyyy"); 
    dispStatus(title, '<p>' + s + '</p>',600,500); 
} 

function dispStatus(title,html,width,height) 
{ 
// Display a modeless dialog box with custom HtmlService content. 
    var title = typeof(title) !== 'undefined' ? title : 'No Title Provided'; 
    var width = typeof(width) !== 'undefined' ? width : 250; 
    var height = typeof(height) !== 'undefined' ? height : 300; 
    var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>'; 
    var htmlOutput = HtmlService 
    .createHtmlOutput(html) 
    .setWidth(width) 
    .setHeight(height); 
    SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title); 
} 
+0

謝謝,Cooper,但是這似乎很難編碼一個時區偏移量(GMT-7),我需要這個工作。動態地從「Europe/London」或「Asia/Qatar」或「Pacific/Tarawa」或「America/New_York」或日曆事件可能開始或結束的任何其他時區。因此,它必須是通用解決方案,任何情況 - 我希望我可以硬編碼它,因爲生活會很簡單,但不幸的是我的用例是動態的。 – kxm

+0

請查看timeanddate.com上的API。它們不是免費的,但它們可能可以幫助您解決問題問題。 – Cooper