time_plus 1.1.6
time_plus: ^1.1.6 copied to clipboard
Extensions for DateTime and Duration to easily add, convert, and work with time in Dart.
Make every microsecond count.
Stop wrestling with DateTime
and Duration
. time_plus
gives you the tools you wish Dart had built inโadd time units, break durations down, convert across scales, and do it all with a clear, predictable API. No dependencies. Just useful extensions that make working with time easier.
Table of Contents #
๐
DateTime
- add โ Add time units to
DateTime
from milliseconds to centuries with.addX()
or.addX
getters. - isSame โ Compare two dates by year, month, day, hour, to microseconds using
isSameX()
. - startOf / endOf โ Get start or end of time units: minute, hour, day, week, month, year.
- next โ Find the next matching
DateTime
by weekday or time of day. - leap โ Check if
DateTime
is in a leap year, leap month, or on leap day (Feb 29). - yesterday / tomorrow โ Get yesterday or tomorrow with
.yesterday
or.tomorrow
getters.
โฑ๏ธ Duration
- add โ Chain any time unit, from microseconds to centuries, with
.addX()
or.addX
getters. - in โ Convert durations into whole units like
inWeeks
,inYears
, orinCenturies
. - only โ Extract the remainder after subtracting larger units (e.g.
onlyMinutes
,onlySeconds
). - without โ Strip full units to isolate whatโs left (e.g. time since midnight).
- Factories โ Create durations from any unit or use built-in constants like
DurationFrom.year
.
Why Use time_plus
? #
โ
Zero dependencies โ Pure Dart. No setup, no third-party overhead.
โ
Lightweight & efficient โ Minimal footprint with production-safe performance.
โ
Powerful time utilities โ Add, compare, normalize, and decompose time with confidence.
โ
Production-grade & tested โ Built for reliability with thorough test coverage.
- Ideal for:
- ๐ Calendar logic โ Scheduling, planning, date math, and recurring events
- ๐งฎ Data handling โ Grouping, filtering, and comparing
DateTime
objects with precision - โฑ๏ธ UI & analytics โ Formatting durations, tracking activity, and displaying time intuitively
time_plus
closes the gaps in Dartโs native DateTime
and Duration
APIs โ making your code easier to write, easier to read, and easier to trust.
โ add
โ Add Time to a DateTime
#
Extend DateTime
just like you wish it worked. These extensions let you safely add milliseconds, hours, days, months, and even centuries โ with proper handling for leap years and end-of-month overflows.
addMilliseconds(int)
/addMillisecond
addSeconds(int)
/addSecond
addMinutes(int)
/addMinute
addHours(int)
/addHour
addDays(int)
/addDay
addWeeks(int)
/addWeek
addMonths(int)
/addMonth
addYears(int)
/addYear
addDecades(int)
/addDecade
(10-year spans)addCenturies(int)
/addCentury
(100-year spans)
All methods return a new DateTime
instance. Month and year-based methods clamp invalid dates (e.g. Feb 30 โ Feb 28/29), ensuring always-valid results.
๐งช Example
final now = DateTime(2024, 2, 29);
final nextYear = now.addYear; // โ 2025-02-28
final chained = now.addYear.addYear.addMonth; // โ 2026-03-29
final future = now.addDecades(2); // โ 2044-02-29
final long = now.addCenturies(1); // โ 2124-02-29
โคด๏ธ Back -> Table of Contents
๐งฉ isSame
โ Compare Temporal Precision #
Quickly compare two DateTime
instances with precision โ from year-level down to microseconds. Each method checks only the relevant granularity, making it ideal for scheduling, display logic, or grouping events.
isSameMicrosecond(DateTime)
isSameMillisecond(DateTime)
isSameSecond(DateTime)
isSameMinute(DateTime)
isSameHour(DateTime)
isSameDay(DateTime)
isSameMonth(DateTime)
isSameYear(DateTime)
isSameDecade(DateTime)
(10-year spans)isSameCentury(DateTime)
(100-year spans)
Each method returns true
if the current DateTime
matches the given one at that specific resolution.
๐งช Example
final a = DateTime(2024, 4, 20, 10, 15);
final b = DateTime(2024, 4, 20, 10, 45);
a.isSameDay(b); // โ true
a.isSameHour(b); // โ true
a.isSameMinute(b); // โ false
โคด๏ธ Back -> Table of Contents
๐งฑ startOf
/ endOf
โ DateTime Boundaries #
Get the exact start or end of any time unit โ minute, hour, day, week, month, or year. These helpers return a new DateTime
aligned to the boundary, ideal for filtering, grouping, or time range logic.
startOfMillisecond
/endOfMillisecond
startOfSecond
/endOfSecond
startOfMinute
/endOfMinute
startOfHour
/endOfHour
startOfDay
/endOfDay
startOfWeek
/endOfWeek
(Monday โ Sunday)startOfMonth
/endOfMonth
startOfYear
/endOfYear
startOfDecade
/endOfDecade
(10-year spans)startOfCentury
/endOfCentury
(100-year spans)
Each result is a fresh DateTime
at the zero point of the boundary (or start of the next one, for endOf...
).
๐งช Example
final now = DateTime(2024, 4, 20, 15, 45, 12);
now.startOfDay; // โ 2024-04-20 00:00:00
now.endOfDay; // โ 2024-04-21 00:00:00
now.startOfWeek; // โ 2024-04-15 00:00:00 (Monday)
now.endOfMonth; // โ 2024-05-01 00:00:00
These methods respect the original timezone (local
or UTC
).
โคด๏ธ Back -> Table of Contents
โญ๏ธ next
โ Find the Next Matching DateTime
#
Easily find the next time that matches a specific weekday or time of day โ skipping the current moment even if it matches. Useful for scheduling recurring events, alarms, or reminders.
nextWeekdayTime(weekday, ...)
โ Returns the next occurrence of a specific weekday (1โ7, MondayโSunday), with optional time.nextTimeOfDay(hour, ...)
โ Returns the next time that matches the given time of day (tomorrow if already passed today).
These respect the original timezone (UTC/local) and guarantee forward-only results.
๐งช Example
final now = DateTime(2024, 4, 20, 15); // Saturday
now.nextWeekdayTime(1, hour: 9); // โ Monday 09:00
now.nextTimeOfDay(hour: 8); // โ Tomorrow 08:00
โคด๏ธ Back -> Table of Contents
๐ธ leap
โ Leap Year, Month, and Day Detection #
Easily check if a DateTime
falls in a leap year, on a leap month (February in a leap year), or on the rarest of all โ leap day itself (Feb 29).
isLeapYear
โtrue
if the year has 366 daysisLeapMonth
โtrue
if itโs February in a leap yearisLeapDay
โtrue
if itโs exactly February 29
๐งช Example
final date = DateTime(2024, 2, 29);
final dateNotDay = DateTime(2024, 2, 28);
date.isLeapYear; // โ true
date.isLeapMonth; // โ true
date.isLeapDay; // โ true
dateNotDay.isLeapDay; // โ false
โคด๏ธ Back -> Table of Contents
๐
yesterday
/ tomorrow
โ Relative Day Helpers #
Convenient shortcuts to get yesterday
, tomorrow
, and their weekday numbers โ always aligned to the start of the day.
yesterday
โ Start of the previous daytomorrow
โ Start of the next daypreviousWeekday
โ Weekday number of yesterday (1 = Monday
,7 = Sunday
)nextWeekday
โ Weekday number of tomorrow
Perfect for logic around schedules, shifts, events, or time-based UI highlights.
๐งช Example
final now = DateTime(2024, 4, 20, 13, 45); // Saturday
now.yesterday; // โ 2024-04-19 00:00:00
now.tomorrow; // โ 2024-04-21 00:00:00
now.previousWeekday; // โ 5 (Friday)
now.nextWeekday; // โ 7 (Sunday)
All results respect the original time zone (local
or UTC
).
โคด๏ธ Back -> Table of Contents
โ add
โ Add Time Units to a Duration #
Easily extend a Duration
by any time unit โ from microseconds to centuries โ using intuitive, chainable methods. Supports both numeric arguments and single-unit getters for clear, readable code.
addMicroseconds(int)
/addMicrosecond
addMilliseconds(int)
/addMillisecond
addSeconds(int)
/addSecond
addMinutes(int)
/addMinute
addHours(int)
/addHour
addDays(int)
/addDay
addWeeks(int)
/addWeek
(7-day spans)addMonths(int)
/addMonth
(Approximation of 30-day months)addYears(int)
/addYear
(Approximation of 365-day years)addDecades(int)
/addDecade
(3650-day spans)addCenturies(int)
/addCentury
(36500-day spans)
All methods return a new Duration
without modifying the original instance.
๐งช Example
final duration = Duration(hours: 1);
final extended = duration.addMinutes(30); // โ 1h 30m
final chained = duration.addDay.addHour; // โ 1d 2h
final future = duration.addDecades(1); // โ 3650d + 1h
โ ๏ธ Note on accuracy:
TheaddMonths
,addYears
,addDecades
, andaddCenturies
methods onDuration
use fixed approximations
(30 days per month, 365 days per year, etc.). These are suitable for rough estimates, timers, or analytics โ
but not for calendar-accurate date math.โ For precise handling of leap years, month lengths, and date rollovers, use the equivalent methods on
DateTime
instead.
โคด๏ธ Back -> Table of Contents
๐งฎ in
โ Convert Duration to Whole Units #
Convert a Duration
into whole, integer-based time units โ perfect for calculations, comparisons, or formatting where only full units matter. These getters are safe, clear, and consistent with calendar approximations.
inWeeks
โ Full 7-day weeksinMonths
โ Full 30-day monthsinYears
โ Full 365-day yearsinDecades
โ Full 10-year spansinCenturies
โ Full 100-year spans
All values are calculated using integer division and return only whole units.
final d = Duration(days: 750);
d.inYears; // โ 2
d.inMonths; // โ 25
โ ๏ธ Note on accuracy:
TheaddMonths
,addYears
,addDecades
, andaddCenturies
methods onDuration
use fixed approximations
(30 days per month, 365 days per year, etc.). These are suitable for rough estimates, timers, or analytics โ
but not for calendar-accurate date math.โ For precise handling of leap years, month lengths, and date rollovers, use the equivalent methods on
DateTime
instead.
โคด๏ธ Back -> Table of Contents
๐งฉ only
โ Break Down Duration by Remaining Units #
Decompose a Duration
into its leftover parts, excluding higher units. Ideal for time formatting (e.g. 1h 23m 45s
) or visual breakdowns.
onlyMicroseconds
โ Microseconds (excluding full milliseconds)onlyMilliseconds
โ Milliseconds (excluding full seconds)onlySeconds
โ Seconds (excluding full minutes)onlyMinutes
โ Minutes (excluding full hours)onlyHours
โ Hours (excluding full days)onlyDays
โ Total full days
These getters let you reconstruct any Duration
piece by piece.
final duration = Duration(days: 1, hours: 2, minutes: 45, seconds: 30);
final days = duration.onlyDays;
final hours = duration.onlyHours;
final minutes = duration.onlyMinutes;
final seconds = duration.onlySeconds;
print('$days days, $hours hours, $minutes minutes, $seconds seconds');
// โ 1 days, 2 hours, 45 minutes, 30 seconds
โคด๏ธ Back -> Table of Contents
๐งผ without
โ Remove Full Units from a Duration #
Strip away full units from a Duration
, leaving only the remainder. Useful when you want the leftover time within a day, hour, minute, or second โ like isolating time of day or breaking durations into parts.
withoutDays
โ Removes full days, keeps hours/minutes/etc.withoutHours
โ Removes full hours, keeps minutes/seconds/etc.withoutMinutes
โ Removes full minutes, keeps seconds/milliseconds/etc.withoutSeconds
โ Removes full seconds, keeps milliseconds/microsecondswithoutMilliseconds
โ Removes full milliseconds, keeps microseconds
Each method returns a new Duration
with the higher units removed.
๐งช Example
final d = Duration(hours: 2, minutes: 45, seconds: 30);
final remainder = d.withoutHours; // โ 0:45:30.000000
โคด๏ธ Back -> Table of Contents
๐๏ธ factories
โ Create Durations from Any Unit #
A unified way to construct Duration
instances from any time unit โ including long spans like months, years, decades, and centuries. Provides both numeric constructors and ready-to-use constants.
microseconds(int)
,milliseconds(int)
,seconds(int)
minutes(int)
,hours(int)
,days(int)
weeks(int)
,months(int)
,years(int)
decades(int)
,centuries(int)
- Predefined constants:
microsecond
,millisecond
,second
,minute
,hour
,day
,week
,month
,year
,decade
,century
These are calendar-aligned approximations:
- 1 week = 7 days
- 1 month = 30 days
- 1 year = 365 days
- 1 decade = 3650 days
- 1 century = 36500 days
๐งช Example
final workout = DurationFrom.minutes(45);
final breakTime = DurationFrom.week; // constant: 7 days
final future = workout + DurationFrom.years(2);
โ ๏ธ Note on accuracy:
TheaddMonths
,addYears
,addDecades
, andaddCenturies
methods onDuration
use fixed approximations
(30 days per month, 365 days per year, etc.). These are suitable for rough estimates, timers, or analytics โ
but not for calendar-accurate date math.โ For precise handling of leap years, month lengths, and date rollovers, use the equivalent methods on
DateTime
instead.
โคด๏ธ Back -> Table of Contents