This repository was archived by the owner on Oct 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathdate_utils.dart
More file actions
168 lines (140 loc) · 4.91 KB
/
date_utils.dart
File metadata and controls
168 lines (140 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
library utils;
import "package:intl/intl.dart";
class Utils {
static final DateFormat _monthFormat = new DateFormat("MMMM yyyy");
static final DateFormat _dayFormat = new DateFormat("dd");
static final DateFormat _firstDayFormat = new DateFormat("MMM dd");
static final DateFormat _fullDayFormat = new DateFormat("EEE MMM dd, yyyy");
static final DateFormat _apiDayFormat = new DateFormat("yyyy-MM-dd");
static String formatMonth(DateTime d) => _monthFormat.format(d);
static String formatDay(DateTime d) => _dayFormat.format(d);
static String formatFirstDay(DateTime d) => _firstDayFormat.format(d);
static String fullDayFormat(DateTime d) => _fullDayFormat.format(d);
static String apiDayFormat(DateTime d) => _apiDayFormat.format(d);
static bool _isMondayFirstDayOfWeek = false;
static const List<String> weekdays = const [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
];
/// The list of days in a given month
static List<DateTime> daysInMonth(DateTime month) {
var first = firstDayOfMonth(month);
var daysBefore = first.weekday;
var firstToDisplay = first.subtract(new Duration(days: daysBefore));
var last = Utils.lastDayOfMonth(month);
var daysAfter = 7 - last.weekday;
var lastToDisplay = last.add(new Duration(days: daysAfter));
return daysInRange(firstToDisplay, lastToDisplay).toList();
}
static bool isFirstDayOfMonth(DateTime day) {
return isSameDay(firstDayOfMonth(day), day);
}
static bool isLastDayOfMonth(DateTime day) {
return isSameDay(lastDayOfMonth(day), day);
}
static DateTime firstDayOfMonth(DateTime month) {
return new DateTime(month.year, month.month);
}
static void setIsMondayFirstDayOfWeek(bool isMonday) {
_isMondayFirstDayOfWeek = isMonday;
}
static DateTime firstDayOfWeek(DateTime day) {
/// Handle Daylight Savings by setting hour to 12:00 Noon
/// rather than the default of Midnight
day = new DateTime.utc(day.year, day.month, day.day, 12);
if (_isMondayFirstDayOfWeek) {
return day.subtract(new Duration(days: day.weekday - 1));
}
/// Weekday is on a 1-7 scale Monday - Sunday,
/// This Calendar works from Sunday - Monday
var decreaseNum = day.weekday % 7;
return day.subtract(new Duration(days: decreaseNum));
}
static DateTime lastDayOfWeek(DateTime day) {
/// Handle Daylight Savings by setting hour to 12:00 Noon
/// rather than the default of Midnight
day = new DateTime.utc(day.year, day.month, day.day, 12);
if (_isMondayFirstDayOfWeek) {
return day.add(new Duration(days: day.weekday - 1));
}
/// Weekday is on a 1-7 scale Monday - Sunday,
/// This Calendar's Week starts on Sunday
var increaseNum = day.weekday % 7;
return day.add(new Duration(days: 7 - increaseNum));
}
/// The last day of a given month
static DateTime lastDayOfMonth(DateTime month) {
var beginningNextMonth = (month.month < 12)
? new DateTime(month.year, month.month + 1, 1)
: new DateTime(month.year + 1, 1, 1);
return beginningNextMonth.subtract(new Duration(days: 1));
}
/// Returns a [DateTime] for each day the given range.
///
/// [start] inclusive
/// [end] exclusive
static Iterable<DateTime> daysInRange(DateTime start, DateTime end) sync* {
var i = start;
var offset = start.timeZoneOffset;
while (i.isBefore(end)) {
yield i;
i = i.add(new Duration(days: 1));
var timeZoneDiff = i.timeZoneOffset - offset;
if (timeZoneDiff.inSeconds != 0) {
offset = i.timeZoneOffset;
i = i.subtract(new Duration(seconds: timeZoneDiff.inSeconds));
}
}
}
/// Whether or not two times are on the same day.
static bool isSameDay(DateTime a, DateTime b) {
return a.year == b.year && a.month == b.month && a.day == b.day;
}
static bool isSameWeek(DateTime a, DateTime b) {
/// Handle Daylight Savings by setting hour to 12:00 Noon
/// rather than the default of Midnight
a = new DateTime.utc(a.year, a.month, a.day);
b = new DateTime.utc(b.year, b.month, b.day);
var diff = a.toUtc().difference(b.toUtc()).inDays;
if (diff.abs() >= 7) {
return false;
}
var min = a.isBefore(b) ? a : b;
var max = a.isBefore(b) ? b : a;
var result = max.weekday % 7 - min.weekday % 7 >= 0;
return result;
}
static DateTime previousMonth(DateTime m) {
var year = m.year;
var month = m.month;
if (month == 1) {
year--;
month = 12;
} else {
month--;
}
return new DateTime(year, month);
}
static DateTime nextMonth(DateTime m) {
var year = m.year;
var month = m.month;
if (month == 12) {
year++;
month = 1;
} else {
month++;
}
return new DateTime(year, month);
}
static DateTime previousWeek(DateTime w) {
return w.subtract(new Duration(days: 7));
}
static DateTime nextWeek(DateTime w) {
return w.add(new Duration(days: 7));
}
}