How to compare dates with different time zones in moment.js

My server's date format is in UTC. I am running the node server in UTC format. I want to check if the current time exceeds 8AM in Indian timezoneie +5.30 and sends mail. How can I determine this usingmoment.js

+4
source share
2 answers

Use moment-time :

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

Or, since India does not use daylight saving time, you actually do not need a time-zone. You just need to specify a fixed offset correctly. Other zones that use DST or have other transitions with a basic offset do need a point in time.

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

, isBefore , . moment().isAfter(...), , .

, UTC , , UTC.

+3

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
}
+1

Source: https://habr.com/ru/post/1623657/


All Articles