如何比较moment.js中不同时区的date

我的服务器的date格式是UTC 。 我以UTC格式运行我的节点服务器。 我想检查当前时间是否在Indian timezone上午8点以上,即+5.30,并发送邮件。 我怎样才能识别这个使用moment.js

使用时区 :

 if (moment.tz("08:00","HH:mm","Asia/Kolkata").isBefore()) { // ... } 

或者,由于印度不使用夏令时,所以您实际上不需要时区。 您只需要正确指定固定的偏移量。 其他使用DST或具有其他基本偏移转换的区域确实需要瞬时时区。

 if (moment.parseZone("08:00+05:30","HH:mmZ").isBefore()) { // ... } 

有了以上isBefore ,请记住,当没有参数时, isBefore将默认为当前时间。 你可以使用moment().isAfter(...)来写,但是反过来它会稍微短一些。

无论您是与UTC还是当地时间进行比较,都无关紧要,因为内部时刻无论如何都会跟踪即时UTC值。

你可以使用isAfter和Moment Timezone :

 // Get server date with moment, in the example serverTime = current UTC time var serverDate = moment.utc(); // Get time in India with Moment Timezone var indiaDate = moment.tz("Asia/Kolkata"); // Setting time to 8:00 AM (I'm supposing you need to compare with the current day) indiaDate.hours(8).minutes(0).seconds(0); if( serverDate.isAfter(indiaDate) ){ // Server date is greater than 8 AM in India // Add here the code to send a mail }