Line data Source code
1 : import 'package:flutter/material.dart';
2 :
3 : ///
4 : ///
5 : ///
6 : extension DateTimeExtension on DateTime {
7 : ///
8 : ///
9 : ///
10 2 : TimeOfDay get time => TimeOfDay.fromDateTime(this);
11 :
12 : ///
13 : ///
14 : ///
15 1 : DateTime mergeStartSeconds({
16 : int second = 0,
17 : int millisecond = 0,
18 : int microsecond = 0,
19 : }) =>
20 1 : copyWith(
21 : second: second,
22 : millisecond: millisecond,
23 : microsecond: microsecond,
24 : );
25 :
26 : ///
27 : ///
28 : ///
29 8 : DateTime mergeStart({
30 : TimeOfDay time = const TimeOfDay(hour: 0, minute: 0),
31 : int second = 0,
32 : int millisecond = 0,
33 : int microsecond = 0,
34 : }) =>
35 8 : copyWith(
36 8 : hour: time.hour,
37 8 : minute: time.minute,
38 : second: second,
39 : millisecond: millisecond,
40 : microsecond: microsecond,
41 : );
42 :
43 : ///
44 : ///
45 : ///
46 1 : DateTime mergeEndSeconds({
47 : int second = 59,
48 : int millisecond = 999,
49 : int microsecond = 0,
50 : }) =>
51 1 : copyWith(
52 : second: second,
53 : millisecond: millisecond,
54 : microsecond: microsecond,
55 : );
56 :
57 : ///
58 : ///
59 : ///
60 6 : DateTime mergeEnd({
61 : TimeOfDay time = const TimeOfDay(hour: 23, minute: 59),
62 : int second = 59,
63 : int millisecond = 999,
64 : int microsecond = 0,
65 : }) =>
66 6 : copyWith(
67 6 : hour: time.hour,
68 6 : minute: time.minute,
69 : second: second,
70 : millisecond: millisecond,
71 : microsecond: microsecond,
72 : );
73 :
74 : ///
75 : ///
76 : ///
77 5 : int get daysInMonth {
78 10 : if (month == DateTime.february) {
79 45 : return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)
80 : ? 29
81 : : 28;
82 : }
83 10 : List<int> days = <int>[31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
84 :
85 15 : return days[month - 1];
86 : }
87 :
88 : ///
89 : ///
90 : ///
91 9 : DateTime get monthFirstDay => copyWith(day: 1).mergeStart();
92 :
93 : ///
94 : ///
95 : ///
96 12 : DateTime get monthLastDay => copyWith(day: daysInMonth).mergeEnd();
97 :
98 : ///
99 : ///
100 : ///
101 2 : DateTime get prevMonthLastDay =>
102 6 : monthFirstDay.subtract(const Duration(days: 1)).mergeEnd();
103 :
104 : ///
105 : ///
106 : ///
107 3 : DateTime get prevMonthFirstDay => prevMonthLastDay.monthFirstDay;
108 :
109 :
110 : ///
111 : ///
112 : ///
113 2 : DateTime get nextMonthFirstDay =>
114 6 : monthLastDay.add(const Duration(days: 1)).mergeStart();
115 :
116 : ///
117 : ///
118 : ///
119 3 : DateTime get nextMonthLastDay => nextMonthFirstDay.monthLastDay;
120 :
121 : ///
122 : ///
123 : ///
124 2 : DateTime weekFirstDay([int firstDay = DateTime.sunday]) => subtract(
125 1 : Duration(
126 6 : days: weekday - firstDay + (weekday - firstDay < 0 ? 7 : 0),
127 : ),
128 1 : ).mergeStart();
129 :
130 : ///
131 : ///
132 : ///
133 2 : DateTime weekLastDay([int lastDay = DateTime.saturday]) => add(
134 1 : Duration(
135 6 : days: lastDay - weekday + (lastDay - weekday < 0 ? 7 : 0),
136 : ),
137 1 : ).mergeStart();
138 : }
|