简体   繁体   English

在JavaScript中将UTC毫秒转换为中央时间

[英]Converting UTC milliseconds to Central Time in javascript

We have a bunch of systems events that are stored in a database with a milliseconds timestamp in UTC. 我们有一堆系统事件,这些事件以UTC的毫秒级时间戳存储在数据库中。 In order for me to get the JSON I need, I just need to send a request like this.... http://xxx.xxx.xxx/gimmeJson?starttime=MILLISECONDS&endtime=MILLISECONDS 为了获取所需的JSON,我只需要发送这样的请求即可。... http : //xxx.xxx.xxx/gimmeJson?starttime=MILLISECONDS&endtime=MILLISECONDS

So when an event happened at 11:00pm CST it has been converted to the UTC millisecond equivalent and stored. 因此,当某个事件在CST下午11:00发生时,它已转换为UTC毫秒并存储。

I am having a big issue wrapping my head around milliseconds because I'm thinking about it like timezones. 我遇到了一个很大的问题,因为我正在像时区一样思考,所以我的头要绕毫秒。

I saw a SO question similar to this here: How do I convert UTC/ GMT datetime to CST in Javascript? 我在这里看到了一个与此类似的问题: 如何将UTC / GMT日期时间转换为Javascript中的CST? (not local, CST always) and it was close but not quite there. (不是本地的,始终CST) ,而且距离很近,但距离那里并不远。

If timestamps are stored in UTC milliseconds, how can I query them for their Central time equivalent? 如果时间戳以UTC毫秒存储,我如何查询它们的等效中部时间? By that I mean my boss wants a report of all events that happened in the central timezone but I have to query a database that has these timestamps stored as UTC milliseconds. 就是说,我的意思是我的老板想要报告中央时区中发生的所有事件,但是我必须查询一个数据库,该数据库将这些时间戳存储为UTC毫秒。

Ultimately I have to come up with ** some ** number to send on a URL for UTC MILLISECONDS that is the equivalent of say "September 24, 12:00:00 Central". 最终,我必须拿出一些**号来发送UTC MILLISECONDS的URL,该URL相当于说“ September 24,12:00:00 Central”。 What compounds this issue is that the web service is fairly new and has been shown to be a bit buggy so I need to make sure I have this right. 使这个问题更复杂的是,Web服务是一个相当新的功能,并且显示出它有点bug,因此我需要确保自己拥有此权利。

// 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");

Can someone confirm I am on the right track? 有人可以确认我在正确的轨道上吗? Many, many thanks in advance. 非常感谢。

There's no such thing as milliseconds timestamp in UTC or any other timezone. 没有UTC或其他任何时区的毫秒时间戳。 Millisecond timestamps are always from 1970-01-01T00:00:00.000Z ; 毫秒时间戳始终从1970-01-01T00:00:00.000Z ; they are zone agnostic. 它们与区域无关。

In order to get the timestamp for the zone you want, simple create a Date instance and use the Date.prototype.getTime method 为了获得所需区域的时间戳,只需创建一个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()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM