Explosion Pills
Explosion Pills

Reputation: 191749

Compare dates from one timezone with dates from another timezone

I have a MySQL database that is running in America/New_York and a node server that is running on UTC. I need to run a query against the MySQL database from node and compare times. The time comparison is pretty close, so I need to do a timezone conversion.

Note: This is a Wordpress database controlled by a third party; I cannot ALTER the database in any way.

Currently, I am just doing this:

comparisonDate = new Date((new Date).setHours(new Date().getHours() - 4)));

Specifically subtracting 4 hours from the node server's UTC date to compare to MySQL.

This works fine, but I'm concerned about what will happen after daylight savings time. I would rather do the conversion (I can guarantee that I know the MySQL server timezone ahead of time).

moment-timezone seems promising, but when I do

moment.tz('America/New_York').toDate()

I get still get the date with UTC time and not EST/EDT time.

How can I do the appropriate timezone conversion consistently in JavaScript?

Upvotes: 0

Views: 92

Answers (2)

Jalpa
Jalpa

Reputation: 720

This may be useful for you.

date_default_timezone_set("America/Los_Angeles");

using this php function you can set default timezone.

Upvotes: 0

Satyam Koyani
Satyam Koyani

Reputation: 4274

I faced the same issue of timezone conversion.

I did like this and it is working fine.

var moment = require('moment');
var timezone = require('moment-timezone');

var now = timezone(moment()).tz("America/New_York"); 

Here now variable gives time of America/New_York.

After daylight savings time moment-timezone takes care of that.

In moment-timezone they have Parsing Ambiguities section which explains this scenario.

Upvotes: 1

Related Questions