Last active
June 21, 2021 10:54
-
-
Save yongjhih/5323ea717a213c1b34e7a9327d33cf11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension DateTimeX<T extends DateTime> on T { | |
DateTime copyWith({ | |
int year, | |
int month, | |
int day, | |
int hour, | |
int minute, | |
int second, | |
int millisecond, | |
int microsecond, | |
}) { | |
return DateTime( | |
year ?? this.year, | |
month ?? this.month, | |
day ?? this.day, | |
hour ?? this.hour, | |
minute ?? this.minute, | |
second ?? this.second, | |
millisecond ?? this.millisecond, | |
microsecond ?? this.microsecond, | |
); | |
} | |
/// Last day of this month | |
DateTime lastDay() => nextMonths().copyWith(day: 0); | |
DateTime lastDaySecond() => nextMonths().copyWith( | |
day: 1, | |
hour: 0, | |
minute: 0, | |
second: -1, | |
millisecond: 0, | |
microsecond: 0, | |
); | |
/// Last day moment of this month | |
/// | |
/// Equivalent to first day moment of next month - 1 microsecond | |
DateTime lastDayMoment() => nextMonths().copyWith( | |
day: 1, | |
hour: 0, | |
minute: 0, | |
second: 0, | |
millisecond: 0, | |
microsecond: -1, | |
); | |
DateTime firstDay() => copyWith(day: 1); | |
/// First day moment of this month | |
DateTime firstDayMoment() => copyWith( | |
day: 1, | |
hour: 0, | |
minute: 0, | |
second: 0, | |
millisecond: 0, | |
microsecond: 0, | |
); | |
/// Last moment today | |
/// | |
/// Equivalent to tomorrow first moment - 1 microsecond | |
DateTime lastMoment() => copyWith( | |
day: day + 1, | |
hour: 0, | |
minute: 0, | |
second: 0, | |
millisecond: 0, | |
microsecond: -1, | |
); | |
/// First moment today | |
DateTime firstMoment() => copyWith( | |
hour: 0, | |
minute: 0, | |
second: 0, | |
millisecond: 0, | |
microsecond: 0, | |
); | |
/// 1234567 | |
/// ^ | |
DateTime firstWeekday([int end]) => copyWith( | |
day: day - (DurationX.daysPerWeek) + 1, | |
); | |
DateTime firstWeekdayMoment([int end]) => copyWith( | |
day: day - (DurationX.daysPerWeek) + 1, | |
hour: 0, | |
minute: 0, | |
second: 0, | |
millisecond: 0, | |
microsecond: 0, | |
); | |
/// TODO end weekday | |
DateTime lastWeekday([int end]) => copyWith( | |
day: day + (DurationX.daysPerWeek) - 1, | |
); | |
/// TODO end weekday | |
DateTime lastWeekdayMoment([int end]) => copyWith( | |
day: day + (DurationX.daysPerWeek), | |
hour: 0, | |
minute: 0, | |
second: 0, | |
millisecond: 0, | |
microsecond: -1, | |
); | |
/// expect(3/31.monthAgo(), 2/29) | |
DateTime monthsAgo([int months = 1]) => copyWith(month: month - months).coerceAtMost(copyWith(month: month - months + 1, day: 0)); | |
/// expect(1/31.nextMonth(), 2/29) | |
DateTime nextMonths([int months = 1]) => copyWith(month: month + months).coerceAtMost(copyWith(month: month + months + 1, day: 0)); | |
DateTime yearsAgo([int years = 1]) => copyWith(year: year - years); | |
DateTime nextYears([int years = 1]) => copyWith(year: year + years); | |
DateTime weeksAgo([int weeks = 1]) => copyWith(day: day - (weeks * DurationX.daysPerWeek)); | |
DateTime nextWeeks([int weeks = 1]) => copyWith(day: day + (weeks * DurationX.daysPerWeek)); | |
DateTime yesterday() => daysAgo(); | |
DateTime tomorrow() => nextDays(); | |
DateTime daysAgo([int days = 1]) => copyWith(day: day - days); | |
DateTime nextDays([int days = 1]) => copyWith(day: day + days); | |
DateTimeRange hours({ bool inclusive = true }) => inclusive | |
? firstMoment().rangeTo(tomorrow().firstMoment(), step: 1.hours) | |
: firstMoment().rangeTo(lastMoment(), step: 1.hours); | |
/// 1234567 | |
/// ^ | |
DateTimeRange weekdays({ bool inclusive = true }) => inclusive | |
? firstMoment().rangeTo(nextWeeks().firstMoment(), step: 1.days) | |
: firstMoment().rangeTo(lastWeekdayMoment(), step: 1.days); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment