Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: toRelative multiple units #579

Closed
marcobiedermann opened this issue Sep 23, 2019 · 6 comments · Fixed by #926
Closed

Feature request: toRelative multiple units #579

marcobiedermann opened this issue Sep 23, 2019 · 6 comments · Fixed by #926

Comments

@marcobiedermann
Copy link
Contributor

It would be great to specify multiple units for toRelative() to receive a more precise result if the date is less than 24 hours.

toRelative({ units: ['days', 'hours' ]})

> 24 hours

in 2 days

< 24 hours

in 10 hours
@GillesDebunne
Copy link
Collaborator

Indeed sounds useful.

Have you tried specifying no unit at all? In that case, the method picks the 'best' unit (i.e. largest non zero unit), which should indeed return in 2 days or in 10 hours I think.

@marcobiedermann
Copy link
Contributor Author

@GillesDebunne

Yes, I've tried but it always falls back to date or today / yesterday if it is within the next / last 24 hours

@GillesDebunne
Copy link
Collaborator

From the unit tests :

const base = DateTime.fromObject({ year: 1983, month: 10, day: 14 });
  expect(base.plus({ minutes: 1 }).toRelative({ base })).toBe("in 1 minute");
  expect(base.plus({ minutes: 5 }).toRelative({ base })).toBe("in 5 minutes");
  expect(base.plus({ minutes: 65 }).toRelative({ base })).toBe("in 1 hour");
  expect(base.plus({ minutes: 165 }).toRelative({ base })).toBe("in 2 hours");
  expect(base.plus({ hours: 23 }).toRelative({ base })).toBe("in 23 hours");
  expect(base.plus({ hours: 24 }).toRelative({ base })).toBe("in 1 day");

What you describe looks more like toRelativeCalendar.

@SidharthRaveendran
Copy link

why does toRelative round down the highest unit whereas toRelativeCalendar rounds up?

@GillesDebunne
Copy link
Collaborator

The difference between these two methods is that toRelative deals with datetime whereas toRelativeCalendar handles dates only. Hence the day granularity for the latter.

@andreialecu
Copy link
Contributor

I opened a PR at #926 to implement this.

andreialecu added a commit to andreialecu/luxon that referenced this issue Apr 30, 2021
icambron pushed a commit that referenced this issue May 8, 2021
* feat: multiple units support in toRelative

closes #579

* add two more tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants