Reputation: 7820
I'm trying to find the best way in JavaScript to get the day of the week of a particular date in any time zone, given only a date string.
Specifically, if I have a date string like:
2022-01-29
And I create a new Date object in JS with that string, and then call the getDay
method on the Date object, because I'm in the "America/New_York" time zone, I actually get the day of the week for the day before that date, not the day of the week of the date itself.
This can be easily demonstrated for anyone in a time zone that is minus-UTC time (e.g., America, Canada, etc.) by running:
new Date('2022-01-29').getDay()
which should return 6
for Saturday, but because of the time zone shift, I get 5
for Friday.
I found "https://stackoverflow.com/a/39209842/128421", which recommended using the getTimezoneOffset
method like:
var date = new Date('2016-08-25T00:00:00')
var userTimezoneOffset = date.getTimezoneOffset() * 60000;
new Date(date.getTime() - userTimezoneOffset);
That does seem to work, but a couple of points:
- userTimezoneOffset
at the end, but it only works for me with + userTimezoneOffset
. Why?What is the best way to get the day of the week in JavaScript when all I have is a date string?
Upvotes: 1
Views: 2170
Reputation: 28196
Use
new Date('2022-01-29').getUTCDay()
new Date('2022-01-29')
will create a Date object with the UTC (Greenwhich) date "2022-01-29" and time "00:00h". When getting a weekday with .getDay()
your browser would calculate it for your local timezone. By using .getUTCDay()
instead you get the weekday for the UTC timezone.
Interesting point made by @RayHatfield
console.log("UTC-time:", new Date("2022-01-29").getUTCHours()) // 0
console.log("UTC-time:", new Date("2022-01-29Z00:00:00").getUTCHours()) // 0 ("Z" is for "ZULU" -> UTC)
console.log("UTC-time:", new Date("2022-01-29T00:00:00Z").getUTCHours()) // 0 (ISO 8601 date string -> UTC)
console.log("UTC-time:", new Date("2022-01-29T00:00:00").getUTCHours()) // UTC time (h) for your local midnight
See https://en.m.wikipedia.org/wiki/ISO_8601 for details on date strings.
Upvotes: 7
Reputation: 147413
If you want to get the weekday name of any date in any timezone, you can use toLocaleString with suitable options.
Because the built–in parser parses '2022-01-29' as UTC, the timezone needs to be set to UTC. If the day name for some other timezone is required, just insert the appropriate IANA representative location.
// Saturday
console.log(new Date('2022-01-29').toLocaleString('en',{
weekday: 'long',
timeZone: 'UTC'
}));
That way you're not trying to manually adjust the date.
The getTimezoneOffset method in the OP works too, even where the offset has changed such as for daylight saving or historic changes, but can be a bit simpler:
let d = new Date('2022-01-29');
d.setMinutes(d.getMinutes() - d.getTimezoneOffset());
console.log(d.getDay()); // 6
console.log(d.toLocaleString('en',{weekday:'long'})); // Saturday
Upvotes: 2
Reputation: 65
let weekday = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][new Date().getDay()]
Upvotes: -3