1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- DateTime addDay(DateTime date, int days) {
- return date.add(Duration(days: days));
- }
- DateTime subtractDay(DateTime date, int days) {
- return date.subtract(Duration(days: days));
- }
- DateTime firstDayOfWeek(DateTime date) {
- if (date.weekday == DateTime.sunday) {
- return date;
- }
- return date.subtract(Duration(days: date.weekday));
- }
- // =================================== 获取一周的数据 ↓ ===================================
- List<DateTime> getWeekdays(DateTime date, int at) {
- final first = firstDayOfWeek(date);
- if (at >= 0) {
- return _addWeek(first, at);
- } else {
- return _subtractWeek(first, -at);
- }
- }
- List<DateTime> _addWeek(DateTime date, int add) {
- final day = addDay(date, 7 * add);
- return _getWeekDaysAt(day);
- }
- List<DateTime> _subtractWeek(DateTime date, int subtract) {
- final day = subtractDay(date, 7 * subtract);
- return _getWeekDaysAt(day);
- }
- List<DateTime> _getWeekDaysAt(DateTime date) {
- return List.generate(7, (index) => index)
- .map((index) => date.add(Duration(days: index)))
- .toList();
- }
- // =================================== 获取二周的数据 ↓ ===================================
- List<DateTime> get2Weekdays(DateTime date, int at) {
- final first = firstDayOfWeek(date);
- if (at >= 0) {
- return _add2Week(first, at);
- } else {
- return _subtract2Week(first, -at);
- }
- }
- List<DateTime> _add2Week(DateTime date, int add) {
- final day = addDay(date, 14 * add);
- return _get2WeekDaysAt(day);
- }
- List<DateTime> _subtract2Week(DateTime date, int subtract) {
- final day = subtractDay(date, 14 * subtract);
- return _get2WeekDaysAt(day);
- }
- List<DateTime> _get2WeekDaysAt(DateTime date) {
- return List.generate(14, (index) => index)
- .map((index) => date.add(Duration(days: index)))
- .toList();
- }
|