Im trying to make an app that counts down the time untill the next friday, but therefore I need the date of the next friday. Any help is deeply appreciated!
extension DateTimeExtension on DateTime {
DateTime next(int day) {
return this.add(
Duration(
days: (day - this.weekday) % DateTime.daysPerWeek,
),
);
}
}
void main() {
var today = DateTime.now();
print(today);
print(today.next(DateTime.friday));
print(today.next(DateTime.friday).weekday == DateTime.friday);
// Works as expected when the next day is after sunday
print(today.next(DateTime.monday));
print(today.next(DateTime.monday).weekday == DateTime.monday);
}
2020-06-24 18:47:40.318
2020-06-26 18:47:40.318
true
2020-06-29 18:47:40.318
true
See this for more information on DateTime
.