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

Luxon seems incredibly illogical #1601

Closed EwaMakowska closed 6 months ago

EwaMakowska commented 6 months ago
  1. I create a new date and set it as UTC (because it has to be UTC)
  2. subtracts 1 day
  3. receives the same date

    let currentDate = DateTime.fromISO(${newDate.year}-${addZero(newDate.month)}-${addZero(newDate.day)}T${addZero(newDate.hour)}:${addZero(newDate.minute)}, {zone: 'UTC'}) // eg. 25.02.2024 23:00 newDate = currentDate.minus({days: 1}) console.log(newDate) // again 25.02.2024 23:00

And I read that it may require further conversions, but what kind? It's really hard to find in the documentation and parts of it were hard to understand) And it looks incredibly illogical. As if you had some kind of bug.

Can you direct me to the appropriate place in the documentation or write how to convert it?

icambron commented 6 months ago

This works just fine for me:

> DateTime.fromISO("2024-02-25T23:00", { zone: "UTC" })
  .minus({ days: 1 })
  .toISO()  // => '2024-02-24T23:00:00.000Z'

No "conversions", no "illogical", etc. I'm not sure what documentation you are looking for. I suspect your inputs are just not what you think they are. Can you try the code above directly?

The one thing I'd add is that you usually don't want to disassemble a structured object, format it into a string, and the parse the result. Your object is already in the form needed by Luxon's fromObject() method. I would do this like:

// this is what you appear to have as input
const input = { year: 2024, month: 2, day: 25, hour: 23, minute: 0 }

// this is what you're making
const newDate = DateTime.fromObject(input, { zone: "UTC" }).minus({ days: 1 };

// here is what i looks like
console.log(newDate) //=> 2024-02-24T23:00:00.000Z

As a hint for future engagements with open source software, it is generally more likely that you have made some mistake than that a widely-used library doesn't work for the basic operations it advertises supporting, and for which thousands of developers use it every day. It is fine to ask for help in such situations, but whining about it in Github issues is irritating, and makes it less likely that people will provide that help.