【发布时间】:2019-10-23 00:52:34
【问题描述】:
数据直接从 firebase\firestore 拉到 vue\nuxt。这是对象的出价。
所以 log.Created 是一个 Firebase 时间跨度,toDate() 使它成为一个日期,然后时刻 from\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)
【问题讨论】:
-
您的要求并不完全清楚。 Firestore 的
toDate()返回一个 JSDate对象,因此在将其传递给 Moment.js 以格式化为相对时间段时,任何时区都应该是无关紧要的。您使用的moment过滤器来自哪里? -
您是否 100% 确定您的时间戳符合您的预期?
Tue Oct 22 2019 17:11:59 GMT-0400是大约 4 小时前 -
我认为你可能是对的,它是转换服务器端,这解释了很多......深入研究它
标签: vue.js google-cloud-firestore momentjs nuxt.js