0

We have a bunch of systems events that are stored in a database with a milliseconds timestamp in 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

So when an event happened at 11:00pm CST it has been converted to the UTC millisecond equivalent and stored.

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? (not local, CST always) and it was close but not quite there.

If timestamps are stored in UTC milliseconds, how can I query them for their Central time equivalent? 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.

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". 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.

// 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.

Community
  • 1
  • 1
Jane Wilkie
  • 1,703
  • 3
  • 25
  • 49

1 Answers1

2

There's no such thing as milliseconds timestamp in UTC or any other timezone. Millisecond timestamps are always from 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

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());
                       
Phil
  • 157,677
  • 23
  • 242
  • 245