在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。
答案 0 :(得分:0)
您的问题由于时区不同而发生。您的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是正确的,您的上一个动作不是“ today”。您应该考虑在所有日期中使用相同的时区。