如何在Vue绑定中将moment(“ from”,“ now”)与时区组合

时间:2019-10-23 00:52:34

标签: vue.js google-cloud-firestore momentjs nuxt.js

数据从firebase \ firestore直接拉到vue \ nuxt。这是该对象的出价。

因此log.Created是一个火力时报的Timepan,toDate()将其设置为日期,然后从\ now的瞬间变为“ 4小时前”

<td class="py-3">{{ log.Created.toDate() | moment("from", "now") }}</td>

问题是,不是4小时前,而是现在,但是UTC没有给我时区偏移量。

我尝试在时刻插件中设置默认时区

import Vue from 'vue'
import VueMoment from 'vue-moment'
import moment from 'moment-timezone'

moment.tz.setDefault(moment.tz.guess())

Vue.use(
    VueMoment, {
    moment,
})

但是我想我可能会在这里划线……

难题中缺少什么?

更多调试数据

<td class="py-3">
  {{ convertDate(log.Created.toDate()) | moment("from", "now") }}<br/><br/>
  {{ convertDate(log.Created.toDate()) }} <br/><br/>
  {{ log.Created.toDate() }}
</td>

功能:

convertDate: function(date){
  return moment(date).tz(moment.tz.guess());
}

...并输出

4 hours ago

Tue Oct 22 2019 17:11:59 GMT-0400

Tue Oct 22 2019 17:11:59 GMT-0400 (Eastern Daylight Time)

1 个答案:

答案 0 :(得分:0)

问题是这里的数据转换代码 https://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

.AddHours(-8)不正确

在此SO上移动到ConvertFromUnixTimestamp How can I convert a DateTime to the number of seconds since 1970? 很棒...

感谢菲尔的帮助