From 815f7a8425748f5c79d2e1fcb361725b2618a2e7 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Mon, 7 Oct 2024 13:35:12 +0200 Subject: [PATCH] v1.0.4 - Added `yesterday` and `tomorrow` extensions to `DateTime` Signed-off-by: Hans Kokx --- CHANGELOG.md | 3 +++ example/lib/main.dart | 6 ++++++ lib/src/extensions/date_time.dart | 31 +++++++++++++++++++++++++++++++ pubspec.yaml | 2 +- 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9c5f4a..794e2ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.0.4 + +- Added `yesterday` and `tomorrow` extensions to `DateTime` ## 1.0.3+2 diff --git a/example/lib/main.dart b/example/lib/main.dart index 1a9e4dc..9f37b9a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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 = " "; diff --git a/lib/src/extensions/date_time.dart b/lib/src/extensions/date_time.dart index 204f716..2b53113 100644 --- a/lib/src/extensions/date_time.dart +++ b/lib/src/extensions/date_time.dart @@ -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; +} diff --git a/pubspec.yaml b/pubspec.yaml index 126cad6..59d2dee 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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