My server's date format is in UTC
. I am running my node server in UTC format. I wanna check whether the current time is greater than 8AM in Indian timezone
i.e +5.30 and should send a mail. How can I identify this using moment.js
Using moment-timezone:
if (moment.tz("08:00","HH:mm","Asia/Kolkata").isBefore()) {
// ...
}
Or, since India does not use daylight saving time, you actually don't need moment-timezone. You just need to specify the fixed offset correctly. Other zones that use DST or have other base-offset transitions to consider will indeed need moment-timezone.
if (moment.parseZone("08:00+05:30","HH:mmZ").isBefore()) {
// ...
}
With both of the above, keep in mind that isBefore
will default to the current time when there are no parameters. You could write it using moment().isAfter(...)
, instead, but it's slightly shorter the other way.
It doesn't matter whether you compare against UTC or local time because internally moment is tracking the instantaneous UTC-based value anyway.
You can use isAfter
and 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With