moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.31k stars 729 forks source link

can't get week day index from UTC date #1597

Closed EwaMakowska closed 6 months ago

EwaMakowska commented 7 months ago

I have a problem with how to get the utc date to get a day of the week from this date. As I understood I have my local date

let dt= DateTime.local(fullYear, month, day, hour, minutes) let dtUTC = dt.toUTC() let dateFromISO = DateTime.fromISO(dtUTC.toString()) //this give wrong date, not UTC but local console.log(dateFromISO.weekday) // is wrong, not from UTC, but local

So, how to do this? You can direct me to the appropriate place in the documentation, unfortunately, when I tried to look for it myself, I did not find anything useful. In general, think about the documentation, because it's terrible and you can't find anything there.

diesieben07 commented 7 months ago

fromISO and other parsing methods still return a DateTime in the local zone. An offset in the ISO is only used to parse the correct time instant, the instant in time is then converted to the local zone. This is a useful default, because normally you don't really know (or care) about the offset specified in the ISO string, you just want to parse it and then present it to the user in a way they understand (their local zone). There are several options available to change this behavior:

  1. Specify the zone option when calling fromISO: DateTime.fromISO(value, { zone: 'UTC' }). This will make fromISO return a DateTime in that zone. Additionally, if the ISO string has no offset specified, it will interpret the date time in that zone.
  2. Specify the setZone option when calling fromISO: DateTime.fromISO(value, { setZone: true }). This will make fromISO return a DateTime with whatever zone was used to parse the ISO string. If you set the zone option, too, it will be that zone. If you don't, it will either be a fixed offset zone based on the ISO string (if it has an offset) or local otherwise.
  3. You can just call toUTC (or setZone if you want something other than UTC) after parsing.

So, regarding your concrete issue, getting the UTC weekday is simple:

const dt = DateTime.fromISO(whatever); // or get the DateTime from wherever you want
const utcWeekday = dt.toUTC().weekday; // always UTC; no matter what dt is

In general, think about the documentation, because it's terrible and you can't find anything there.

Unfortunately critique like this is not helpful. Contributions regarding the documentation are very welcome, both in terms of constructive issues outlining concrete problems (not "it's terrible") as well as pull requests improving it.

icambron commented 7 months ago

I'll add that while many aspects of the documentation could of course be improved, the docs on this particular thing seem pretty good: https://moment.github.io/luxon/#/zones?id=strings-that-specify-an-offset. The relevant information is organized under "Zones and offsets" > "Creating DateTimes" > "Strings that specify an offset" (I recommend reading all of that "Creating DateTimes" section). Since you have a zone-related issue and you are creating a DateTime using a string that specifies an offset, that seems like a very reasonable place to look.