0

const d = new Date(); // get 'now'
d.getDay(); // 0 = Sunday, 1 = Monday... in my browser's timezone.
d.toLocaleDateString('en-GB', { timeZone: 'NZ' }); // formatted date in NZ

console.log(d);

How can I get the numeric day in NZ? toLocaleDateString does not support weekday: 'numeric'.

The only way I can think of is to map the day to Sat, Sun ... and then use a {Sun: 0...} map to map it back to a number?!

EDIT: To be clear: the day-of-the week for a given time will be different in different timezones. But Date.getDay() only returns the numeric date in the browser's timezone. toLocaleDateString can present the date in different timezones, however it does not appear to support outputting the day/weekday as a number, which seems a surprising omission.

Also, I do not want to use some deprecated massive library (looking at you, moment) but instead I want to use vanilla Javascript.

artfulrobot
  • 20,637
  • 11
  • 55
  • 81
  • Does this answer your question? [How to get the day of week and the month of the year?](https://stackoverflow.com/questions/4822852/how-to-get-the-day-of-week-and-the-month-of-the-year) – vale Apr 29 '22 at 21:15
  • @vale no, not at all. – artfulrobot Apr 29 '22 at 21:16
  • Won't this be different for different locales. E.g. some regions start the week on Sunday, whereas others start on Monday – Alicia Sykes Apr 29 '22 at 21:20
  • @Lissy93 the [getDay() method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) produces a machine-standard 0 = Sun, ... 6 = Sat, *regardless of the browser's locale*. I want that function, but for a given timezone. (your deleted answer returns a string as my third line of code does...) – artfulrobot Apr 29 '22 at 21:22
  • 1
    You can use the trick from this answer: [Javascript how to verify day by getDay when using timezone](https://stackoverflow.com/questions/57187691/javascript-how-to-verify-day-by-getday-when-using-timezone) – create a date from the formatted date (keep in mind it relies on 'en-US' encoding as `dd/mm/yyyy` isn't parsed correctly by `new Date()`) You're probably better off mapping against your own object. – pilchard Apr 29 '22 at 21:38
  • 1
    @artfulrobot this seems to be (as you rightly stated) an omission as no "numeric" format is provided in`toLocaleDateString` for `weekday`. – Mohsen Alyafei Apr 29 '22 at 22:15
  • 1
    @artfulrobot for now, seems the only way is to map the weekdays to numbers. – Mohsen Alyafei Apr 29 '22 at 22:17

1 Answers1

0

OK, as of April 2022, apparently there is no way! Here's my work-around:

// This date is Wednesday 2022-04-20 13:00:00 at UTC/GMT
// In NZ, it is Thursday  2022-04-21 01:00:00
const d = new Date(Date.UTC(2022, 3, 20, 13, 0, 0));

const timeZone = 'NZ';
const numericDayInNZ =
  ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
  .indexOf(d.toLocaleDateString('en-GB', {weekday:'short', timeZone}));

  console.log("At UTC date: ", d,
    "your timezone has a numeric day of:", d.getDay(),
    "and in NZ it is", numericDayInNZ);
      
  • Note: if you want to use this code in your own locale, you can either change the en-GB and all the day names, or just leave it as-is if that's unoffensive and all you need is the number.

  • The handy thing is that you can rearrange the days, e.g. to put Sun last if you wanted 0 = Monday.

  • I think it was an oversight in the spec not to have included a Date.prototype.getDay equivalent in Date.prototype.toLocale(Date|Time)?String functions, but at least this solution is efficient enough and does not require bulky third party libs.

artfulrobot
  • 20,637
  • 11
  • 55
  • 81