user2243832
user2243832

Reputation: 59

Compare different time zone date in Jquery

How to compare dates from different time zone?

for e.g.

x = "2013-12-02T10:10:17-0400" // (timezone EST) 

and compare this date to current date

var d = new Date(); // timezone(PST)

check x < d ?

Upvotes: 0

Views: 1255

Answers (3)

Venemo
Venemo

Reputation: 19107

When it comes to dealing with dates and times in JavaScript, I usually use Moment.js which is a library exactly for this purpose.

Its URL is http://momentjs.com/

Then you can simply parse the given string with this line:

// Parse the given datetime
var mydate = moment("2013-12-02T10:10:17-0400");

And you can also compare two different moment values:

// Compare given datetime with the current datetime
if (moment("2013-12-02T10:10:17-0400") > moment()) {
    // ...
}

Or you can just convert it to a regular JavaScript Date object:

// Parse given datetime and convert to Date object
var mydate = moment("2013-12-02T10:10:17-0400").toDate();
// Compare to current datetime
if (mydate > (new Date())) {
    // ...
}

Note that the unary + operator also works with moment objects just as you would expect. So +moment() outputs the same as +(new Date()).

It's also very well documented, the Moment.js docs page has a ton of examples and useful info about it.

Upvotes: 1

srgstm
srgstm

Reputation: 3759

Use this javascript library to manipulate dates in different time zones: https://github.com/mde/timezone-js

It uses the TZ database: http://en.wikipedia.org/wiki/Tz_database

Upvotes: 0

Praveen
Praveen

Reputation: 56529

  1. Better convert any of the dates to a common timezone(better to have UTC)
  2. Now convert the datetime to milliseconds
  3. Compare the milliseconds

Hope you understand

Upvotes: 0

Related Questions