【问题标题】:Date comparison works locally, but not on heroku日期比较在本地有效,但在 heroku 上无效
【发布时间】:2019-10-06 00:00:12
【问题描述】:

在一个 Vuejs 应用程序中,我目前有一个每天可用一次的按钮。单击一次后,它将被禁用,直到第二天。这在本地运行良好,但在 heroku 上,比较似乎不起作用。

这是 vue 组件中的 y 日期计算值,如果 Act 是在当前日期 00:00 之后创建的,则返回 true:

  computed: {
    ...
    actedToday() {
      if (!!this.$store.getters.lastAct) {
        let now = new Date();
        console.log('this is now in actedToday computed: ', now);
        let lastActDate = new Date(this.$store.getters.lastAct.created_at);
        console.log('this is last act date in computed actedToday', this.$store.getters.lastAct.created_at);
        console.log('was last Act today? ', lastActDate.getTime() > now.setHours(0,0,0,0));

        return lastActDate.getTime() > now.setHours(0,0,0,0)
      }
    }
  }, 

这是 console.log 在本地返回的内容:

this is now in actedToday computed:  Fri Oct 04 2019 15:20:24 GMT-0400 (Eastern Daylight Time)
ActButton.vue?0bb7:31 this is last act date in computed actedToday 2019-10-04T19:20:23.901Z
ActButton.vue?0bb7:32 was last Act today?  true

这是来自 heroku 应用程序的类似日志:

this is now in actedToday computed:  Fri Oct 04 2019 15:21:18 GMT-0400 (Eastern Daylight Time)
ActButton.vue:31 this is last act date in computed actedToday 2019-10-04T03:30:22.266Z
ActButton.vue:32 was last Act today?  false

在第一个示例中,比较按预期进行。第二种,虽然两个日期在同一天,但比较返回false。

【问题讨论】:

    标签: datetime vue.js heroku


    【解决方案1】:

    您的问题是由于不同的时区而出现的。您的 lastActDate 是祖鲁时间(UTC,请注意末尾的 Z),但您的 actedToday 是 EDT。使用setHours(0,0,0,0) 将为您提供一天的开始,但在同一时区。因此,您具有以下值:

    //locally
    now = Fri Oct 04 2019 15:20:24 GMT-0400 (Eastern Daylight Time)
    now.setHours(0,0,0,0) = Fri Oct 04 2019 00:00:00 GMT-0400 (Eastern Daylight Time)
    lastActDate = 2019-10-04T19:20:23.901Z = Fri Oct 04 2019 15:20:23.901 GMT-0400 (Eastern Daylight Time)
    
    //heroku
    now = Fri Oct 04 2019 15:21:18 GMT-0400 (Eastern Daylight Time)
    now.setHours(0,0,0,0) = Fri Oct 04 2019 00:00:00 GMT-0400 (Eastern Daylight Time)
    lastActDate = 2019-10-04T03:30:22.266Z = Fri Oct 03 2019 23:30:22.266 GMT-0400 (Eastern Daylight Time)
    

    因此 heroku 是正确的,你的最后一个动作不是“今天”。您应该考虑在所有日期中使用相同的时区。

    【讨论】:

    • 谢谢。我想问题是如何在祖鲁时间开始一天,或者反过来如何将 LastActDate 转换为 EDT,所以我是在比较苹果和苹果,对吧?
    • 我不确定 vue.js 和你的项目,但也许可以看看这里:forum.vuejs.org/t/…
    猜你喜欢
    • 2020-11-04
    • 2022-07-24
    • 2017-11-27
    • 1970-01-01
    • 2017-03-26
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    相关资源
    最近更新 更多