kind people of StackOverflow, I'm having a problem with luxon (I guess I set it up wrong or did something wrong with it), right now it can't calculate future dates (says NaN), and for past dates take years into account so you get something like this:
[
what I want this code to do is focus on month and day (forget about the year) and if the passed date is 7 days away or is less than 7 days away from today's date say "the passed date is <= a week", later this will trigger a notifier, that will notify me a week in advance of my friends & families bdays.
the code:
const isBirthdayThisWeek = (birthDate) => {
const endDate = DateTime.now()
const startDate = DateTime.fromISO(birthDate)
const interval = Interval.fromDateTimes(startDate, endDate)
const dateDifference = interval.length('days')
const wholeNumberedDateDifference = Math.round(dateDifference)
wholeNumberedDateDifference <= 7
? console.log('bday is in less than a week', wholeNumberedDateDifference)
: wholeNumberedDateDifference > 7
? console.log('bday is more than in a week', wholeNumberedDateDifference)
: console.log('something went wrong', wholeNumberedDateDifference)
}
Thank you all in advance.
You can use the magnitude of a compute duration. Durations work for both intervals into the future and past. Adjust the start date to "snap" to the same current year so the date diff doesn't cross year boundary.
import { DateTime } from "luxon";
const startDate = DateTime.fromISO("2000-04-26").set({
year: DateTime.now().get("year")
});
const diff = Math.abs(startDate.diffNow().as('day'));
const wholeNumberedDateDifference = Math.round(diff);
if (diff <= 7) {
console.log("bday is in less than a week", wholeNumberedDateDifference);
} else {
console.log("bday is more than in a week", wholeNumberedDateDifference);
}
bday is in less than a week 2