Reputation: 1512
If I have a string of the following format on the server, which could be in any timezone:
var timeString = 2014-05-24T14:00:00.000Z
What is the best way to to compare the hour represented by the string with the hour that a client may pass in as part of a GET request (Node.js)?
I need help because the date on the server and the date received from the client may be in Daylight Savings time, or in different timezones.
I tried passing in the client time as the number of milliseconds since Unix epoch:
var clientTime = new Date(parseInt(req.query.localTime));
var serverTime = new Date(timeString);
if (serverTime.getUTCHours() == clientTime.getUTCHours()) {
// the hours match
}
But it doesn't seem to work out.
Upvotes: 1
Views: 2219
Reputation: 243
Have you checked out moment.js, it makes date parsing stupid easy and has a companion library for dealing with timezone conversion (but not usually needed). There is a section in their documentation that explains dealing with UTC vs local and how to convert back and forth.
http://momentjs.com/docs/#/parsing/utc/
var moment = require('moment')
var timeString = '2014-11-14T00:06:34.000Z'
var epochTime = 1415923594000
var serverTime = moment(timeString).utc()
var clientTime = moment(epochTime).utc()
console.log(serverTime.hour());
console.log(clientTime.hour())
Upvotes: 1