Compare two dates using Date component only disregarding time

4.8k views Asked by At

In C#, typically you can use DateTime.Today to get today's date disregarding the time component (e.g. the time component would be at midnight basically).

How do I do that using Luxom library?

I understand you can write DateTime.local() to get a Date object with current Time included, but is there no simple way to effectively disregard the time component so that I can perform a comparison with another Date object?

I can't find any relevant information in the Luxon documentation, though I may have missed something.

In standard JS, this would do it:

new Date(new Date().setHours(0,0,0,0));

But that seems awkward?

2

There are 2 answers

0
VincenzoC On BEST ANSWER

You can startOf('day') to set midnight to given DateTime:

"Set" this DateTime to the beginning of a unit of time.

then you can use toJSDate() to get the equivalent Javascript Date or other method like toISO, toISODate(), toFormat() etc to get the string representation of a DateTime.

Example:

const DateTime = luxon.DateTime;
console.log( DateTime.local().startOf('day').toJSDate() );
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.js"></script>

If you need to compare Luxon objects have a look at Comparing DateTimes section of the manual.

0
Ciaran Gallagher On
DateTime.local().toISODate();

This gave me what I wanted.