12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import 'package:shared/utils/date_time_utils.dart';
- DateTime addDay(DateTime date, int days) {
- return date.add(Duration(days: days));
- }
- DateTime subtractDay(DateTime date, int days) {
- return date.subtract(Duration(days: days));
- }
- //一周的第一天从哪里开始,Sun 还是 Mon
- DateTime firstDayOfWeek(DateTime date) {
- //返回星期天开始
- // if (date.weekday == DateTime.sunday) {
- // return date;
- // }
- // return date.subtract(Duration(days: date.weekday));
- // 从星期一开始
- return date.subtract(Duration(days: date.weekday - 1));
- }
- //是否包含选中的日期
- bool hasSelectedDate(List<DateTime> list, DateTime selected) {
- for (DateTime date in list) {
- // 格式化日期并进行比较
- if (DateTimeUtils.formatDate(date, format: 'yyyyMMdd') == DateTimeUtils.formatDate(selected, format: 'yyyyMMdd')) {
- return true;
- }
- }
- return false;
- }
- // 检查日期是否小于今天
- bool isPastDate(DateTime now, DateTime date) {
- // 获取当前日期的凌晨时间
- DateTime todayMidnight = DateTime(now.year, now.month, now.day);
- // 判断给定日期是否小于今天的凌晨时间
- return date.isBefore(todayMidnight);
- }
- // =================================== 获取一周的数据 ↓ ===================================
- 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();
- }
|