esp*_*ora 5 javascript timezone node.js momentjs
我的服务器的日期格式是UTC.我正在以UTC格式运行我的节点服务器.我想检查当前时间是否大于8AM,Indian timezone即+5.30并且应该发送邮件.我该如何识别这个moment.js
使用时刻时区:
if (moment.tz("08:00","HH:mm","Asia/Kolkata").isBefore()) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
或者,由于印度不使用夏令时,实际上你不需要时刻时区.您只需要正确指定固定偏移量即可.其他使用DST或具有其他基本偏移转换的区域确实需要时刻 - 时区.
if (moment.parseZone("08:00+05:30","HH:mmZ").isBefore()) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
对于上述两种情况,请记住,isBefore当没有参数时,默认为当前时间.你可以使用moment().isAfter(...)相反的方式来编写它,但是相反,它会稍微缩短一些.
无论您是将UTC还是本地时间进行比较都无关紧要,因为无论如何内部时刻都在跟踪基于UTC的瞬时值.
// 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
}
Run Code Online (Sandbox Code Playgroud)