2015-10-22 60 views
0

我已經試過尋找答案,儘管我找到的答案非常相似,但我不認爲它們正是我正在尋找的東西。請原諒我,如果這已經在別處回答。在javascript中解析UTC ISO日期到本地時間日期/ jquery

我想解析JavaScript中的ISO日期,以便我可以將它與客戶機系統日期進行比較,並根據ISO日期是在客戶機系統日期之前還是之後顯示信息。

這很好,直到我需要支持IE8,現在我卡住了。

我創建了一個函數,因爲我有三個不同的日期,我需要這樣做。

例如,我的ISO日期是:2015-12-22T11:59 UTC時間。

但是,一旦我的日期被解析,在本地時間的全天時間是11:59,無論我測試哪個時區,它在該時區總是11.59。

我知道我創建的函數目前沒有對時區做任何事情,這是我卡住的地方。我不知道要添加什麼才能將我的結束日期更改爲客戶機時區的反映。

任何幫助或建議將不勝感激。 我無法使用像moments.js這樣的內容,因爲我有上傳限制。

雖然Jquery可用。或純javascript。

<script> 
function setSaleContent() { 


    //creating a new date object that takes the clients current system time. so we can compare it to the dates stored in our array 
    var currentDate = new Date(); 
    console.log(currentDate + " this is the clients date "); 


    //These variables actually come from an external array object, but I'm putting them in here like this for this example. 
    var destinations = { 
     freedate: "2015-12-16T11:59", 
     courierdate: "2015-12-22T11:59", 
     nextdaydate: "2015-12-23T11:59", 
    } 

    //fetch all the ISO dates from the array. 
    var freeDateISO = destinations["freedate"]; 
    var courierDateISO = destinations["courierdate"]; 
    var nextdayDateISO = destinations["nextdaydate"]; 


    //I am creating this reusable function to split up my ISO date for the sake of IE8.. and create it into a date format that can be compared against another date. I know that this isn't doing anything with my timezone and that is where my problem lies. 
    function parseDate(str) { 
     var parts = /^(\d{4}).(\d{2}).(\d{2}).(\d{2}):(\d{2})/.exec(str); 
     if (parts) { 
      return new Date(parts[1], parts[2] - 1, parts[3], parts[4], parts[5]); 
     } 
     return new Date(); 
    } 


    //I would like this date to change to reflect the time zone of the clients system time. 
    //currently returns the date at 11.59 regardless of timezone. 
    //If i was in GMT i would want it to say 11.59 
    //If i was in CT time I would like this to say 05.59 
    //If i was in Perth I would like this to say 19:59 
    var freeDate = parseDate(freeDateISO); 
    console.log(freeDate + " this is the converted date for IE") 





} 


window.onload = setSaleContent; 

+0

不要使用'new Date(...)'(它從本地值創建日期時間),使用'new Date(Date.UTC(...))'作爲utc值! – Bergi

回答

0

的簡單的解決方案是要追加Z到ISO日期,以表明它是在UTC時間,如2015-12-22T11:59Z

當JavaScript將該日期解析爲字符串時,它會自動將UTC日期轉換爲本地時區。

雖然這很簡單,但形式爲new Date(str);的解析調用足夠簡單,但對於使用針對IE8和其他舊瀏覽器的數字參數的解析調用,它不會很好。

一種解析ISO日期與時區填充工具存在:Javascript JSON Date parse in IE7/IE8 returns NaN
這可以做一些修改後更換您的自定義功能parseDate採取一個輸入字符串。

或者,使用新創建的日期中的.getTimezoneOffset()方法來實現您自己的自定義日期操作符來計算當地時區,該日期以分鐘爲單位給出時區偏移量,但您必須想出一種利用由於JavaScript日期對象的方法有限,因此需要調整時間和分鐘等偏移量。

+0

我試圖將Z添加到它之前,並且當我正在執行新的Date(str)時,它完美地工作。但是這在IE8中不起作用。 – w3littlebird

+0

我預計這會發生是因爲您的自定義解析代碼爲IE8。就像我在答案中所說的那樣,如果你不能使用任何庫,你必須用'.getTimezoneOffset()'做些事情並實現你自己的日期操作方法。你可以嘗試用'+00:00'而不是'Z'來追加它,看看IE8是否可以將它解析爲一個字符串,但我懷疑是否有更好的解決方案。或者,請參閱此IE7/8 polyfill解析ISO日期:http://stackoverflow.com/questions/11020658/javascript-json-date-parse-in-ie7-ie8-returns-nan – Ryan

+0

對不起,試圖在iPad上回復並搞砸了我的評論。我試圖將Z附加到它之前,當我在做新的Date(str)並且完成我所需要的時候,它完美地工作,但是這在IE8中不起作用。這就是我創建parseDate函數的原因。我知道我的函數沒有對日期部分做任何事情來獲取時區,我不知道如何寫它,或者如果我需要做一些getTimeZoneOffset計算。但無論我需要做什麼,我都不知道要寫什麼才能實現它,而且我已經嘗試了幾個小時並搜索了幾個小時。 – w3littlebird

相關問題