我已經試過尋找答案,儘管我找到的答案非常相似,但我不認爲它們正是我正在尋找的東西。請原諒我,如果這已經在別處回答。在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;
不要使用'new Date(...)'(它從本地值創建日期時間),使用'new Date(Date.UTC(...))'作爲utc值! – Bergi