简体   繁体   English

如何在JavaScript中比较两种不同的日期格式和不同的时区?

[英]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? 如何用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). 在JavaScript中,使用Date.parse(myDateObject)将不同的日期格式转换为Unix时间戳(尽管最后的3位数字不是unix时间戳的一部分,而是毫秒)。

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 本机JavaScript

You can use the Date.parse(dateObject) to convert your date to a Unix timestamp and do your test on it. 您可以使用Date.parse(dateObject)将日期转换为Unix时间戳并对其进行测试。 You can look at the documentation about it . 您可以查看有关它文档

Using an additionnal library 使用附加库

You can also use an external library, such as Moment.js . 您还可以使用外部库,例如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/ Moment网站: 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. 不要期望Date对象正确或一致地解析日期和时间字符串。 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': 要解析UTC ISO 8601格式的字符串,例如“ 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: 要解析“ Mon Sep 15 2014 18:20:52 GMT + 0200(CEST)”这样的字符串,需要做更多的工作:

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. 您也可以只使用普通的getHourgetMinute等方法,但UTC可以清楚地表明您使用的是相同的偏移量。

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. 将小时,分钟和秒转换为秒,应用偏移量,考虑天边界并比较结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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