简体   繁体   English

JS:如何比较从UTC到定义的时区的2个日期?

[英]JS: How to compare 2 dates converting from UTC to defined timezone?

I stored a string of UTC Date (without time): 我存储了一个UTC日期字符串(没有时间):

var limitDateStr = "2017-07-04T00:00:00.000Z";

And a TIMEZONE in hours (based on this) , not in Zone Name (like America/Antigua) 还有一个以小时为单位的TIMEZONE (基于此) ,而不是区域名称(例如America / Antigua)

var timezone = "-6.0"; //could be from -12.0 to 12.0 

The problem is: Verify if actual date, is greater than limitDate in the same Timezone. 问题是: 验证实际日期是否大于同一时区中的limitDate。


What I Do with Moment.js 我对Moment.js的处理

I Used moment for change the timezone, but when y change the offset. 我使用矩来更改时区,但是当y时更改偏移量。 The date is changed too: 日期也被更改:

  var _expirationDateUTC = moment.utc(limitDateStr).utcOffset(parseFloat(timezone)).format();
  // "2017-07-03T18:00:00-06:00"

Instead of 2017-07-04T00:00:00-06:00 代替2017-07-04T00:00:00-06:00

Actual date, works great: (except for Days Savings) 实际日期,效果很好:(节假日除外)

var today = moment.utc().utcOffset(parseFloat(timezone)).format();
//"2017-07-04T15:32:49-06:00"

And I want to compare... maybe with moment function: 我想比较...也许与矩函数:

today.isAfter(_expirationDateUTC);

but the expiration date is now working like expect. 但是到期日期现在像预期的那样工作。

Ok, there problem seems to be this: 好的,似乎是这样的问题:

var _expirationDateUTC = moment.utc(limitDateStr).utcOffset(parseFloat(timezone)).format();

where limitDateStr = "2017-07-04T00:00:00.000Z" ; 其中limitDateStr = "2017-07-04T00:00:00.000Z" ;

What you are doing up to this point is feeding moment with this date and then apply the timezone offset, which will substract the timezone(in this case -6) to the date you are actually feeding. 到目前为止,您正在做的事情是使用该日期喂养时刻,然后应用时区偏移量,这会将时区(在这种情况下为-6)减去您实际喂养的日期。

"2017-07-04T00:00:00.000Z" - 6 hours = "2017-07-03T18:00:00-06:00" “ 2017-07-04T00:00:00.000Z”-6小时=“ 2017-07-03T18:00:00-06:00”

What you are looking for is not to calculate the limitDate in a different timezone but to have limitDate to be the same as the utc but in a different timezone. 您要查找的不是在不同的时区中计算limitDate,而是使limitDate与utc相同,但在不同的时区中。

So you need this block of code which will add the hours from the target zone: 因此,您需要以下代码块,这些代码块将增加目标区域的小时数:

var limitDateStr = moment("2017-07-04T00:00:00.000Z");
var timezone = "-6.0"

//Substract the timezone offset to the time;
//This should be: "2017-07-04T06:00:00.000Z"
limitDateStr.subtract({hours: parseFloat(timezone)});

//Now you can apply the offseting
//and this will become: "2017-07-04T00:00:00-06:00"
var _expirationDateUTC = moment.utc(limitDateStr).utcOffset(parseFloat(timezone)).format();

var today = moment.utc().utcOffset(parseFloat(timezone)).format();

var hasExpired = today.isAfter(_expirationDateUTC);

Primitive solution was: "add the difference in hours" 原始解决方案是:“加上小时的差异”

function validateDateBefore(date, timezone){
  /** enable this for end day limit **/
  //var _expirationDateUTC = moment.utc(date).utcOffset(parseFloat(timezone)).startOf('hour').add((parseFloat(timezone) * -1) + 23, 'hours').add(59, 'minute').add(59, 'second').format();
  var _expirationDateUTC = moment.utc(date).utcOffset(parseFloat(timezone)).startOf('hour').add((parseFloat(timezone) * -1), 'hours').format();
  var _todayUTC = moment.utc().utcOffset(parseFloat(timezone)).format();

  var today = moment(_todayUTC);
  var expiration = moment(_expirationDateUTC);

  return expiration.isAfter(today);
}

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

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