How to set Luxon DateTime to start of day (reset time)
Asked Answered
M

1

21

I have trawled the docs, but cannot find a a helper to reset the time of a Luxon DateTime.

I can start with a standard date:

export const getDateOnly = (date: Date = new Date()) =>  new Date(
  date.getFullYear(),
  date.getMonth(),
  date.getDate()
)

export const today = () => DateTime.fromJSDate(getDateOnly());

but this seems like a common scenario and I might have just missed it?

Millwater answered 11/7, 2022 at 8:36 Comment(2)
moment.github.io/luxon/api-docs/index.html#datetimestartof?Diglot
yes! just also found it and came back here :)Millwater
V
34

Reposting as answer for future reference.

https://moment.github.io/luxon/api-docs/index.html#datetimestartof

With example:

export const today = () => DateTime.now().startOf('day');
Vince answered 28/7, 2022 at 13:25 Comment(2)
it works with the plus function as well, so if you want to get tomorrow as a date, starting at 00:00:00 it will be like so: const tomorrow = DateTime.now().plus({ days: 1 }).startOf('day');Chambliss
I would even say DateTime.now().toUTC().startOf('day') for the UTC start of todayMurk

© 2022 - 2024 — McMap. All rights reserved.