time_plus 1.1.5 copy "time_plus: ^1.1.5" to clipboard
time_plus: ^1.1.5 copied to clipboard

Extensions for DateTime and Duration to easily add, convert, and work with time in Dart.

img

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.
  • 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, or inCenturies.
  • 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


๐Ÿธ 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 days
  • isLeapMonth โ€” true if itโ€™s February in a leap year
  • isLeapDay โ€” 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 day
  • tomorrow โ€” Start of the next day
  • previousWeekday โ€” 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:
The addMonths, addYears, addDecades, and addCenturies methods on Duration 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 weeks
  • inMonths โ€” Full 30-day months
  • inYears โ€” Full 365-day years
  • inDecades โ€” Full 10-year spans
  • inCenturies โ€” 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:
The addMonths, addYears, addDecades, and addCenturies methods on Duration 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/microseconds
  • withoutMilliseconds โ€” 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:
The addMonths, addYears, addDecades, and addCenturies methods on Duration 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


๐Ÿ”— License MIT ยฉ Jozz #

โ˜• Enjoying this package? You can support it here.

33
likes
0
points
689
downloads

Publisher

verified publisherjozz.biz

Weekly Downloads

Extensions for DateTime and Duration to easily add, convert, and work with time in Dart.

Repository (GitHub)
View/report issues

Topics

#date #time #duration #datetime #date-time

License

unknown (license)

More

Packages that depend on time_plus