Get milliseconds of date in specific timezone

Moment.js is a great choice if you want to work with dates and times in JavaScript. It is very handy for creating interchange representations like ISO 8601 strings, taking your current timezone into account.

Convert to milliseconds

Let’s say you live in Berlin (Germany) and you want to represent June, 26th of 2019 at midnight in milliseconds with Central European Summer Time (GMT+2). With Moment.js v2.24 you have at least the following possibilities to do that:

Defined UTC Offset

defined-utc-offset.js
1
2
3
const moment = require('moment');
const unixTimestamp = moment('2019-06-26T00:00:00.000+02:00').valueOf();
console.log('unixTimestamp', unixTimestamp); // 1561500000000

Detected UTC Offset

detected-utc-offset.js
1
2
3
4
5
const moment = require('moment');
const utcOffsetInMinutes = new Date().getTimezoneOffset(); // -120 (2 hours)
const utcOffsetInMillis = utcOffsetInMinutes * 60000;
const unixTimestamp = moment('2019-06-26T00:00:00.000Z').valueOf() + utcOffsetInMillis;
console.log('unixTimestamp', unixTimestamp); // 1561500000000

Defined Zone Info

defined-zone-info.js
1
2
3
4
const moment = require('moment-timezone');
const zoneInfo = 'Europe/Berlin';
const unixTimestamp = moment.tz('2019-06-26 00:00:00', zoneInfo).valueOf();
console.log('unixTimestamp', unixTimestamp); // 1561500000000

Detected Zone Info

detected-zone-info.js
1
2
3
4
const moment = require('moment-timezone');
const zoneInfo = moment.tz.guess();
const unixTimestamp = moment.tz('2019-06-26 00:00:00', zoneInfo).valueOf();
console.log('unixTimestamp', unixTimestamp); // 1561500000000

Pro Tip: You can get date and time expressed according to ISO 8601 in JavaScript from the current date when calling:

1
new Date().toISOString(); // 019-07-03T13:12:18.784Z

Note that the above string will always be normalized to UTC time (Z), so you need to apply the UTC offset yourself.