我们有一堆系统事件存储在一个数据库中,其中包含一个UTC的毫秒时间戳。为了让我获得我需要的JSON,我只需要发送这样的请求.... http://xxx.xxx.xxx/gimmeJson?starttime=MILLISECONDS&endtime=MILLISECONDS
因此,当事件发生在CST的晚上11点时,它已被转换为UTC毫秒等效值并存储。
我有一个很大的问题包围我的脑袋几毫秒,因为我正在考虑它像时区。
我在这里看到了一个与此类似的问题:How do I convert UTC/ GMT datetime to CST in Javascript? (not local, CST always)它很接近但不完全存在。
如果时间戳以UTC毫秒存储,我如何查询它们的中心时间等效值?我的意思是我的老板想要报告中心时区发生的所有事件,但我必须查询一个数据库,这些时间戳存储为UTC毫秒。
最终我必须提出**一些**号码来发送UTC MILLISECONDS的URL,相当于“9月24日中午12:00:00”。这个问题的复杂性在于Web服务是相当新的,并且已被证明有点错误,所以我需要确保我有这个权利。
// construct a moment object with Central time input
var m = moment('2015-01-01 00:00:00').format('x');
// convert using the TZDB identifier for GMT
m.tz('Europe/London');
// format output however you desire
var s = m.format("x");
有人能证实我走在正确的轨道上吗? 很多,非常感谢提前。
答案 0 :(得分:2)
UTC或任何其他时区都没有毫秒时间戳。毫秒时间戳始终来自1970-01-01T00:00:00.000Z
;他们与区域无关。
要获取所需区域的时间戳,只需创建Date
实例并使用Date.prototype.getTime
方法
var cstTime = new Date('2016-09-24T12:00:00-06:00');
console.log('CST in ISO 8601:', cstTime.toISOString());
console.log('CST timestamp:', cstTime.getTime());
var localTime = new Date();
console.log('Local time in ISO 8601:', localTime.toISOString());
console.log('Local timestamp:', localTime.getTime());