Reputation: 6206
I have array of timezones and I want to know which one is the 'earliest one' where earliest is the one in the earliest time zone.
For example:
Than X is the earliest among them.
I'm using momentjs and momentjs-timzone in NodeJS (not the browser package)
I'm trying to compare the millisecond timestamp but this give me the same value for both:
if (momenttz().tz(timezoneX).format('x') > momenttz().tz(timezoneY).format('x'))
Upvotes: 1
Views: 863
Reputation: 348
In NodeJS syntax it can accomplished by formatting timezones to offset values, finding the minimum component and returning its Timezone value:
var moment = require('moment-timezone');
var inputTzArray = ['Pacific/Chatham', 'America/St_Johns', 'Asia/Jerusalem', 'America/New_York', 'America/Los_Angeles'];
var tzOffsets = []
for (var i in inputTzArray) {
var s = moment.tz(inputTzArray[i]).format('Z'); //format to offset
tzOffsets[i] = parseFloat(s.replace(':','.')); //parse int offset
}
console.log(tzOffsets);
var minIndex = tzOffsets.indexOf(Math.min.apply(Math, tzOffsets)); //find minimum's index
console.log(inputTzArray[minIndex]); //Output earliest timezone
A live demo by running "node server.js" in the interactive console in the link: http://runnable.com/VJ3M3XdZAVg-EVOP
Upvotes: 1