- Added `yesterday` and `tomorrow` extensions to `DateTime`

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2024-10-07 13:35:12 +02:00
parent 3f80193f36
commit 815f7a8425
4 changed files with 41 additions and 1 deletions
+3
View File
@@ -1,3 +1,6 @@
## 1.0.4
- Added `yesterday` and `tomorrow` extensions to `DateTime`
## 1.0.3+2
+6
View File
@@ -12,6 +12,12 @@ void main() {
debugPrint("$today"); // true
debugPrint("$notToday"); // false
final DateTime yesterday = DateTime(0).yesterday;
final DateTime tomorrow = DateTime(0).tomorrow;
debugPrint("Yesterday: $yesterday");
debugPrint("Tomorrow: $tomorrow");
// Strings
const String? nullString = null;
const String emptyString = " ";
+31
View File
@@ -109,3 +109,34 @@ extension FirstDayOfWeek on DateTime {
return firstDay;
}
}
/// An extension that provides convenient getters to retrieve the start of
/// yesterday and tomorrow for `DateTime`.
///
/// This extension simplifies the process of getting the start of the previous day
/// or the next day relative to the current date and time.
extension YesterdayAndTomorrow on DateTime {
/// Returns a `DateTime` representing the start of the previous day (yesterday).
///
/// This getter subtracts one day from the current date and time, then returns
/// the result with the time set to the start of the day (00:00:00).
///
/// Example usage:
/// ```dart
/// DateTime yesterday = DateTime(0).yesterday;
/// ```
DateTime get yesterday =>
DateTime.now().subtract(const Duration(days: 1)).startOfDay;
/// Returns a `DateTime` representing the start of the next day (tomorrow).
///
/// This getter adds one day to the current date and time, then returns the
/// result with the time set to the start of the day (00:00:00).
///
/// Example usage:
/// ```dart
/// DateTime tomorrow = DateTime(0).tomorrow;
/// ```
DateTime get tomorrow =>
DateTime.now().add(const Duration(days: 1)).startOfDay;
}
+1 -1
View File
@@ -1,7 +1,7 @@
name: arcane_helper_utils
description: Provides a variety of helpful utilities and extensions for Flutter
and Dart.
version: 1.0.3+2
version: 1.0.4
repository: https://github.com/hanskokx/arcane_helper_utils
issue_tracker: https://github.com/hanskokx/arcane_helper_utils/issues