Reputation: 20184
I want to see if the current time is greater than Monday of this week at 5:00 pm mountain time, no matter what timezone this code is running in. I've got something like this:
function isAfterMondayEvening() {
var now = new Date();
var mondayEvening = dateFns.setHours(dateFns.setDay(now, 1), 17);
return dateFns.compareAsc(now, mondayEvening);
}
console.log(isAfterMondayEvening());
<script src="//cdn.rawgit.com/date-fns/date-fns/a0005af7d1c3f70c88b8e619bfdff4bf85122863/dist/date_fns.js"></script>
If the server or browser this is running in is in a different timezone, then it will compare the time to Monday at 5 in their timezone. I want to to be compared to Monday at 5 pm in mountain time, no matter what timezone this code runs in. How I can do this?
Upvotes: 2
Views: 2989
Reputation: 4953
When you say "mountain time", are you always talking about Mountain Standard Time or are you wanting to account for Mountain Daylight Time as well? If you're always talking about Mountain Standard Time, you can ignore my answer since your case is simpler. I'm going to address the possibility that you want to account for Mountain Daylight Time as well, which is a more complex problem.
If you're taking daylight time into account, keep in mind that not every place inside the Mountain Time zone observes Mountain Daylight Time, so you actually need to know the location inside the Mountain Time zone (e.g., is it Arizona or Utah?). Once you know that, you need a library like Moment Timezone that has adequate time zone information. You then have to find the most accurate tz database timezone name and use it, with the help of Moment Timezone to figure out the actual UTC time for "Monday of this week at 5:00 pm mountain time in this particular location". Your code would look something like this:
// 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');
}
Upvotes: 0
Reputation: 147373
The US Mountain Time (MT) offset is -0700. Javascript dates are UTC at heart, so all you need to do is compare the internal UTC time value to MT. By "Monday of this week" I presume you want true for Sunday and Monday up to 17:00. If you just mean Monday, then remove the d.getUTCDay() == 0 ||
part.
function isBefore1700MT(date) {
// Copy date so don't affect original
var d = new Date(date);
// Adjust internal UTC to MT
d.setUTCHours(d.getUTCHours() - 7);
// Return true if the UTC day is Sunday or Monday before 17:00
return d.getUTCDay() == 0 || (d.getUTCDay() == 1 && d.getUTCHours() < 17);
}
// Test using current local date and time
var date = new Date();
console.log(date.toString() + ' : ' + isBefore1700MT(date));
Upvotes: 0
Reputation: 191
If you are cool with IE 10+, use luxon (https://moment.github.io/luxon/index.html)
function isAfterMondayEvening() {
const currentTime = luxon.DateTime.local().setZone('America/Denver');
const mondayEvening = currentTime.set({weekday: 1, hour: 17}).startOf('hour');
return currentTime > mondayEvening;
}
Upvotes: 1
Reputation: 606
Assuming you can figure out this week's Monday year/month/day indexes, I think you can do this without libraries. The trick is to represent your known Mountain time in terms of 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}`
);
Upvotes: 0
Reputation: 1918
A method I have used is to normalize both dates to GMT. Assuming your server date is in GMT already, you can convert the browser time to GMT by subtracting the timezone offset.
For example, I am in Atlantic Standard Time (GMT+4). To get the current time as if I was in GMT, I use the formula:
2018-02-09T15:00:00+0400 - (4 * 60 * 60 * 1000) = 2018-02-09T15:00:00Z`
...where 4 is the offset in hours.
Specifically in 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
In JS, Date#getTimezoneOffset
returns the offset in minutes so I omit an extra * 60
.
Upvotes: 1