Reputation: 3443
I'm just getting started with luxon and JavaScript's 'Date'. I use the following date format: Sat Feb 05 2022 00:00:00 GMT+0200 (Eastern European Standard Time)
. I want to compare the dates with today's date and get the days difference from this format Sat Feb 05 2022 00:00:00 GMT+0200 (Eastern European Standard Time)
. However, I am receiving decimal days '2.5154091898148145' when my expected output is two days. I don't how to fix it.
Also I want to get next dates and previous dates.
Thank you in advanced.
const { DateTime } = require("luxon");
const today = new Date();
const day =
"Sat Feb 05 2022 00:00:00 GMT+0200 (Eastern European Standard Time)";
const date1 = DateTime.fromISO(new Date(day).toISOString());
const diff = date1.diff(DateTime.now(), ["days"]).toObject();
console.log(diff.days);
// Output
// 2.520294085648148
// Expected output
// 1. Compare day should be 2 days
// 2. Find next day('2022-02-06') and previous day(('2022-02-04')) from `day` variable
// 3. Return date format like this: 5.2.2022 from `day` variable
Upvotes: 4
Views: 3357
Reputation: 4473
You can just use Math.round()
or Math.floor()
on the result as needed:
const { DateTime } = require("luxon");
const today = new Date();
const day =
"Sat Feb 05 2022 00:00:00 GMT+0200 (Eastern European Standard Time)";
const date = DateTime.fromISO(new Date(day).toISOString());
const diff = date.diff(DateTime.now(), ["days"]).toObject();
const days = Math.floor(diff.days)
console.log(days); //2
Upvotes: 4