【问题标题】:parsing a UTC ISO date to a local time date in javascript/jquery将 UTC ISO 日期解析为 javascript/jquery 中的本地时间日期
【发布时间】:2023-03-23 14:38:01
【问题描述】:

我已经尝试寻找答案,虽然我找到了非常相似的答案,但我不认为它们正是我想要的。如果这已经在其他地方得到回答,请原谅我。

我正在尝试在 javascript 中解析 ISO 日期,以便我可以将其与客户端系统日期进行比较,并根据 ISO 日期是在客户端系统日期之前还是之后显示信息。

这很好,直到我需要支持 IE8,现在我被卡住了。

我创建了一个函数,因为我需要对三个不同的日期执行此操作。

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

但是一旦我的日期被解析,完整的日期是当地时间 11:59,无论我测试哪个时区,在那个时区它总是 11.59。

我知道我当前创建的函数对时区没有任何作用,这就是我卡住的地方。我不知道要添加什么来更改我的结束日期以反映客户端机器的时区。

任何帮助或建议将不胜感激。 因为我有上传限制,所以我无法使用诸如 moment.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值!

标签: javascript jquery date parsing iso


【解决方案1】:

简单的解决方案是将Z附加到ISO日期以表明它是UTC时间,例如2015-12-22T11:59Z

当 JavaScript 将该日期解析为字符串时,它会自动将 UTC 日期转换为本地时区。

虽然使用new Date(str); 形式的解析调用很简单,但它不会很好地与针对IE8 和其他旧浏览器的数字参数的解析调用配合使用。

存在用于解析带时区的 ISO 日期的 polyfill:Javascript JSON Date parse in IE7/IE8 returns NaN
这可以在一些修改后替换您的自定义 parseDate 函数以获取输入字符串。

或者,实现您自己的自定义日期操作程序,以在新创建的日期上使用 .getTimezoneOffset() 方法来处理本地时区,该方法以分钟为单位给出时区偏移量,但您必须想出一种方法来利用由于 JavaScript 日期对象的方法有限,例如调整小时和分钟的偏移量。

【讨论】:

  • 我之前尝试将 Z 附加到它,并且在我执行 new Date(str) 时效果很好。但这在 IE8 中不起作用。
  • 我预计会发生这种情况,因为您的 IE8 自定义解析代码。就像我在答案中所说的那样,如果您不能使用任何库,您将不得不使用 .getTimezoneOffset() 并实现自己的日期操作方法。您可以尝试使用+00:00 而不是Z 附加它,看看IE8 是否可以将其解析为字符串,但我怀疑是否有更好的解决方案。或者,查看这个 IE7/8 polyfill 来解析 ISO 日期:stackoverflow.com/questions/11020658/…
  • 对不起,我试图在 iPad 上回复并弄乱了我的评论。我之前尝试将 Z 附加到它,当我在做 new Date(str) 并且完全符合我的需要时,它工作得很好,但这在 IE8 中不起作用。这就是我创建 parseDate 函数的原因。我知道我的函数没有对日期部分做任何事情来获取时区,而且我不知道如何以可能发生的方式编写它,或者我是否需要进行一些 getTimeZoneOffset 计算。但是无论我需要做什么,我都不知道要写什么来实现它,而且我已经尝试了几个小时并在谷歌上搜索了几个小时。
  • 尝试使用我在上一条评论中提到的 polyfill 替换/增强您的自定义 parseDate 函数,看看它是否有效。从 polyfill 中的正则表达式来看,它应该支持时区并且应该可以解决您的问题。
猜你喜欢
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多