'DateTime remove saturday and sunday

I am creating a todo where the default date should be set to + 3 days but only weekdays. I'm using vue so think a computed of some sort should work?

DateTime.utc().plus({ days: 3 }).toFormat('yyyy-MM-dd HH:mm:ss'),

So how can I exclude Saturday and Sunday here? I've tried googling but haven't been able to find a solution..



Solution 1:[1]

the best solution is to use the package luxon-business-day

but if you don't want to add anything you can :

  • calculate date of next saturday
  • see if you can add expecting number of day
  • if not, add difference to day to add

const dayToAdd = 3;
const DateTime = luxon.DateTime;

var nextSaturday = DateTime.utc().startOf('week').plus({ week: 1 }).minus({days: 2});
var dayDifference = nextSaturday.diff(DateTime.utc(), ["days"]).toObject().days;


var addAfterWeekend = 0;
if (dayDifference < dayToAdd) {
  addAfterWeekend = dayToAdd - dayDifference;
}

var date = DateTime.utc().plus({ days: dayToAdd + addAfterWeekend });

console.log(date.toFormat('yyyy-MM-dd HH:mm:ss'));
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1