我正在尝试制作一个应用程序来倒计时直到下周五,但因此我需要下周五的日期。
非常感谢任何帮助!
Solution
extension DateTimeExtension on DateTime {
DateTime next(int day) {
return this.add(
Duration(
days: (day - this.weekday) % DateTime.daysPerWeek,
),
);
}
}
Testing
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);
}
Output
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欲了解更多信息DateTime
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)