time_plus 1.1.4
time_plus: ^1.1.4 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 any unit to a
DateTime, from milliseconds to centuries, with overflow-safe.addX()or.addXgetters. - ๐งฉ isSame โ Compare two dates by year, month, day, hour, and down to microseconds with
isSameX(). - ๐งฑ startOf/endOf โ Get the start or end of any time unit โ minute, hour, day, week, month, or year.
- ๐ธ leap โ Determine if a
DateTimeis in a leap year, leap month, or on leap day (Feb 29).
- โ Add โ Add any unit to a
-
โฑ๏ธ
Duration+- โ Add โ Chain any time unit, from microseconds to centuries, with
.addX()or.addXgetters. - ๐๏ธ Factories โ Create durations from any unit or use built-in constants like
DurationFrom.year. - ๐งฎ 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).
- โ Add โ Chain any time unit, from microseconds to centuries, with
โ 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)/addMillisecondaddSeconds(int)/addSecondaddMinutes(int)/addMinuteaddHours(int)/addHouraddDays(int)/addDayaddWeeks(int)/addWeekaddMonths(int)/addMonthaddYears(int)/addYearaddDecades(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/endOfMillisecondstartOfSecond/endOfSecondstartOfMinute/endOfMinutestartOfHour/endOfHourstartOfDay/endOfDaystartOfWeek/endOfWeek(Monday โ Sunday)startOfMonth/endOfMonthstartOfYear/endOfYearstartOfDecade/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
๐ธ 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โtrueif the year has 366 daysisLeapMonthโtrueif itโs February in a leap yearisLeapDayโtrueif itโs exactly February 29
๐งช Example
final date = DateTime(2024, 2, 29);
date.isLeapYear; // โ true
date.isLeapMonth; // โ true
date.isLeapDay; // โ true
โคด๏ธ 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)/addMicrosecondaddMilliseconds(int)/addMillisecondaddSeconds(int)/addSecondaddMinutes(int)/addMinuteaddHours(int)/addHouraddDays(int)/addDayaddWeeks(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
โคด๏ธ 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
โคด๏ธ 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);
โคด๏ธ Back -> Table of Contents