Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JavaScript日期与特定时区中的日期进行比较?_Javascript_Date_Timezone - Fatal编程技术网

如何将JavaScript日期与特定时区中的日期进行比较?

如何将JavaScript日期与特定时区中的日期进行比较?,javascript,date,timezone,Javascript,Date,Timezone,我想看看当前时间是否大于本周星期一下午5:00山区时间,无论代码运行在哪个时区。我有这样的想法: // Use whatever time and timezone you're trying to compare with. const targetDate = moment() .tz('America/Denver') .day(1) // Monday .hour(17) // 5pm .minute(0) .second(0) .millisecond(0)

我想看看当前时间是否大于本周星期一下午5:00山区时间,无论代码运行在哪个时区。我有这样的想法:

// Use whatever time and timezone you're trying to compare with.
const targetDate = moment()
  .tz('America/Denver')
  .day(1) // Monday
  .hour(17) // 5pm
  .minute(0)
  .second(0)
  .millisecond(0)
  .toDate();

if (new Date() > targetDate) {
  console.log('after target time');
} else {
  console.log('before target time');
}
函数IsAfterMonday黄昏(){
var now=新日期();
var monday傍晚=dateFns.setHours(dateFns.setDay(现在,1),17);
返回日期fns.compareAsc(现在,星期一晚上);
}
log(isafterMonday黄昏())

我使用的一种方法是将两个日期标准化为GMT。假设您的服务器日期已经是GMT,您可以通过减去时区偏移量将浏览器时间转换为GMT

例如,我在大西洋标准时间(GMT+4)。要获得当前时间,就像我在格林尼治标准时间一样,我使用以下公式:

2018-02-09T15:00:00+0400 - (4 * 60 * 60 * 1000) = 2018-02-09T15:00:00Z`
…其中4是以小时为单位的偏移量

特别是在JS中:

const browserOffset = new Date().getTimezoneOffset();
const timeFromServer = getTimeFromServer();
const currentTimeAsGmt = new Date(Date.now() - (browserOffset * 60 * 1000));

// Now compare timeFromServer (already in GMT) to currentTimeAsGmt
在JS中,
Date#getTimezoneOffset
以分钟为单位返回偏移量,所以我省略了额外的
*60

当你说“山地时间”时,你是一直在谈论山地标准时间,还是也要考虑山地白天时间?如果你总是谈论山区标准时间,你可以忽略我的回答,因为你的情况更简单。我将讨论一种可能性,即你想考虑山地昼时,这是一个更复杂的问题

如果考虑到白天,请记住并非山区时区内的每个地方都遵守山区白天,因此您实际上需要知道山区时区内的位置(例如,是亚利桑那州还是犹他州?)。一旦你知道了这一点,你就需要一个拥有足够时区信息的图书馆。然后,您必须找到最准确的时间,并使用它,借助矩时区计算出“本周星期一下午5:00山区时间在该特定位置”的实际UTC时间。您的代码如下所示:

// Use whatever time and timezone you're trying to compare with.
const targetDate = moment()
  .tz('America/Denver')
  .day(1) // Monday
  .hour(17) // 5pm
  .minute(0)
  .second(0)
  .millisecond(0)
  .toDate();

if (new Date() > targetDate) {
  console.log('after target time');
} else {
  console.log('before target time');
}

假设您可以计算出本周的周一年/月/日索引,我认为您可以在没有库的情况下完成这项工作。诀窍是用UTC表示已知的山地时间:

let utcMonday = new Date(Date.UTC(2018, 2 - 1, 5, 24));
let cstDate = new Date();
let currentTimeGreater = cstDate > utcMonday;

// The rest isn't needed, but shows some console logs of what is going on

let locale = "en-US";
let timeFormatOptions = {
  weekday: "short",
  hour: "numeric",
  minute: "numeric",
  timeZoneName: "long",
  timeZone: "America/Denver"
};

let formattedMountainMonday = utcMonday.toLocaleDateString(
  locale,
  timeFormatOptions
);

let formattedCurrentTime = cstDate.toLocaleDateString(
  locale,
  timeFormatOptions
);

console.log(formattedMountainMonday);
console.log(
  `Current time in relation to Mountain time: ${formattedCurrentTime}`
);
console.log(
  `Current time is greater than this week's Mountain Monday at 5:00 PM: ${currentTimeGreater}`
);

如果你对IE 10+很酷,请使用luxon()

美国山地时间(MT)偏移为-0700。Javascript日期本质上是UTC,所以您需要做的就是将内部UTC时间值与MT进行比较。通过“本周的星期一”,我假设您希望周日和星期一17:00之前为true。如果您只是指星期一,那么删除
d.getUTCDay()==0 |
部分

函数在1700MT(日期)之前{
//复制日期,以免影响原始日期
var d=新日期(日期);
//将内部UTC调整为MT
d、 setUTCHours(d.getUTCHours()-7);
//如果UTC日期为17:00之前的星期日或星期一,则返回true
返回d.getUTCDay()==0 | |(d.getUTCDay()==1和d.getUTCHours()<17);
}
//使用当前本地日期和时间进行测试
变量日期=新日期();

log(date.toString()+':'+isBefore1700MT(date))您尝试过使用React.js吗?日期为UTC。要获得等效的UTC时间戳,请使用toISOString或带有手动格式的UTC方法。美国山区时间在一年中的部分时间仅为7小时的偏移量。在夏令时,这是一个6小时的补偿。请注意,如果您使用此脚本,它可能会在一年中的部分时间返回不正确的结果。@Aaronius并非山区的所有地方都遵守夏时制(例如亚利桑那州的大部分地区)。在夏时制期间,它被称为山地夏时制(MDT),否则称为山地标准时间或仅为山地时间。