我已经试过寻找答案,尽管我找到的答案非常相似,但我不认为它们正是我正在寻找的东西。请原谅我,如果这已经在别处回答。在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