简体   繁体   中英

How can I compare two different date formats and different time zones in JavaScript?

I want to compare two different time objects given in different formats and time zones.

Input as structured english:

IF "Mon Sep 15 2014 18:20:52 GMT+0200 (CEST)" is greater than "2014-09-04T13:57:33.338Z"

Expected output:

True

How to achieve this in JavaScript?

In JavaScript use Date.parse(myDateObject) to convert different date formats to a Unix timestamp (although the resulting last 3 digits are not part of the unix timestamp, but are milliseconds).

eg:

if (Date.parse("Mon Sep 15 2014 18:20:52 GMT+0200 (CEST)") > Date.parse("2014-09-04T13:57:33.338Z")) {}; // evaluates to true

Native JavaScript

You can use the Date.parse(dateObject) to convert your date to a Unix timestamp and do your test on it. You can look at the documentation about it .

Using an additionnal library

You can also use an external library, such as Moment.js . This library allows you to compare your date without transforming them into timestamps or other exoctic formats.

You just have to get your date and compare it to another one:

moment("Mon Sep 15 2014 18:20:52 GMT+0200 (CEST)").isAfter('2014-09-04T13:57:33.338');

I let you explore this wonderful library by yourself if you want.

Moment website: http://momentjs.com/

Hope this two solutions will help you :)

Do not expect the Date object to correctly or consistently parse date and time strings. There are a number of libraries to help if you have many different formats to support, but if you only have one or two then supporting them is not hard.

To parse an UTC ISO 8601 format string like '2014-09-04T13:57:33.338Z':

function parseISOUTC(s) {

  // Get the parts
  var b = s.split(/\D+/);

  // Return a new Date
  return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6]));
}

To parse a string like 'Mon Sep 15 2014 18:20:52 GMT+0200 (CEST)' takes a little more work:

function parseMDYHMSO(s) {

  // Get the parts
  var b = s.split(/[ :]/);

  // Convert the month name to a number
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
  var month = months[b[1].toLowerCase()];

  // Get the offset
  var sign = s.indexOf('+') == -1? 1 : -1;
  var offset = b[7].substr(4,2) * 60;
  offset += +b[7].substr(6,2);

  // Create a new Date
  return new Date(Date.UTC(b[3], month, b[2], b[4], +b[5] + sign*offset, b[6]));
}

So now you have two date objects. If you want to compare just the time part, then get the hours, minutes and seconds of each and compare them:

var d0 = parseISOUTC('2014-09-04T13:57:33.338Z');

// 2014-09-15T16:20:52.000Z
var d1 = parseMDYHMSO('Mon Sep 15 2014 18:20:52 GMT+0200 (CEST)');

d0.getUTCHour(); // 13
d1.getUTCHour(); // 16

d0.getUTCMin(); // 57
d1.getUTCMin(); // 20

d0.getUTCMin(); // 33
d1.getUTCMin(); // 52

You can also just use the plain getHour , getMinute , etc. methods but the UTC ones make it clear that you're using the same offset.

You can also do the above just using the time part with offset and ignore the date. Convert the hours, minutes and seconds to say seconds, apply the offset, allow for day boundaries and compare the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM