时刻时区将时间戳调整为时区并返回值 [英] moment timezone adjust timestamp to a timezone and return value

查看:116
本文介绍了时刻时区将时间戳调整为时区并返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用moment-timezone修改时间戳记时区,我想使用已应用的偏移量获取修改后的值

I try to modify timestamps timezone with moment-timezone and I want to get the modified value with the applied offset

var newTimestamp = momentTz.tz(timestamp, 'Europe/Berlin')

这就是我尝试的

    var results = {};

    for (var timestamp in timestamps) {
         var commitCount = timestamps[timestamp];
         console.log(typeof timestamp)
         console.log(timestamp)
         console.log(moment(timestamp).format())
         //console.log(moment.tz(timestamp, 'Europe/Berlin'))
         //results[ moment.tz(timestamp, 'Europe/Berlin')] = commitCount;
   };

这是日志的摘录

string
1528063200
Invalid date

如何获取新的时间戳?

推荐答案

您的timestamp值似乎是包含 Unix时间值,以秒为单位.您可以立即使用X格式说明符对其进行解析.

It looks like your timestamp values are strings that contain Unix Time values, in terms of seconds. You can parse them with the X format specifier in moment.

但是,您无法获得以时区调整的这种格式的时间戳.这是因为Unix时间值始终基于UTC.他们在时区中是荒谬的.任何这样的值都将是错误的.

However, you cannot get a timestamp in this format that is adjusted for a time zone. That is because Unix Time values are always based on UTC. It is nonsensical for them to be in a time zone. Any such value would be in error.

可以但是发出基于字符串的格式,例如 ISO 8601 -当您不向format函数提供任何参数时,这是默认设置.

You can however emit a string-based format, such as ISO 8601 - which is the default when you don't supply any arguments to the format function.

简而言之:

moment.tz('1528063200', 'X', 'Europe/Berlin').format() //=> '2018-06-04T00:00:00+02:00'

这篇关于时刻时区将时间戳调整为时区并返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆