【问题标题】:How to combine moment("from", "now") with timezone in a Vue binding如何在Vue绑定中将时刻(“from”,“now”)与时区结合起来
【发布时间】: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() 返回一个 JS Date 对象,因此在将其传递给 Moment.js 以格式化为相对时间段时,任何时区都应该是无关紧要的。您使用的moment 过滤器来自哪里?
  • 您是否 100% 确定您的时间戳符合您的预期? Tue Oct 22 2019 17:11:59 GMT-0400大约 4 小时前
  • 我认为你可能是对的,它是转换服务器端,这解释了很多......深入研究它

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


【解决方案1】:

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

.AddHours(-8) 不正确

在此 SO 上移至 ConvertFromUnixTimestamp How can I convert a DateTime to the number of seconds since 1970? 效果很好...

感谢菲尔的帮助

【讨论】:

    猜你喜欢
    • 2021-04-17
    • 2019-10-11
    • 2023-03-21
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2013-09-16
    • 2018-12-08
    相关资源
    最近更新 更多