【问题标题】:Vue-moment showing wrong time with timezone setVue-moment 显示设置时区的错误时间
【发布时间】:2022-01-12 14:46:09
【问题描述】:

不知道我做错了什么。我有一个 laravel 8 应用程序,我在 UTC 中使用 postgresql 中的 timestamp_no_timezone 存储我的时间。当我查看数据库中的时间时,它显示的是今天的日期,时间为 13:45。当我使用 vue-moment 并显示时区设置为 America/New_York 的时间时,它仍然显示下午 1:45,它应该是上午 8:45 的偏移量。我会错过什么?我什至已经做了一个时区的console.log,它显示的是America/New_York。

这是我正在使用的表达式:

{{ [ timesheet.start, "YYYY-MM-DD HH:mm:ss" ] | moment("timezone", "America/New_York", "h:mm A") }}

【问题讨论】:

    标签: javascript laravel vue.js momentjs inertiajs


    【解决方案1】:

    在发送响应时将存储的日期时间转换为纪元。

    如何将 dateTime 转换为 epoch 有很多方法(在 epoch 中发送 dateTime 作为响应)。

    添加你的模型

    protected $casts = ['datetime_field' => 'timestamp']
    

    将此方法添加到您发送响应的 Laravel 类中

        protected function toEpoch($datetime )
        {
            if (gettype($datetime) == 'string') {
                $datetime = new DateTime($datetime);
                return Carbon::instance($datetime)->format('U');
            } else if ($datetime instanceof Carbon) {
                return $datetime->format('U');
            } else if ($datetime != null) {
                return Carbon::instance($datetime)->format('U');
            }
          } 
        }
    

    在vue项目中

    {{new Date(epoch*1000)}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 2018-03-20
      • 2010-12-08
      相关资源
      最近更新 更多