Time class
Represents an immutable point in time, wrapping Dart's DateTime.
Provides convenience methods for comparison, manipulation, formatting, and integration with custom date/time part types (Year, Month, Day, Weekday). All operations that modify the time return a new Time instance.
- Implemented types
Constructors
- Time.new(DateTime dateTime)
-
Creates a Time instance directly from a DateTime object.
const
- Time.now()
-
Creates a Time instance representing the current moment.
factory
- Time.todayAt(int hour, int minute, [int second = 0, int millisecond = 0, int microsecond = 0])
-
Creates a Time instance for today's date at the specified time.
factory
Properties
- dateTime → DateTime
-
Returns the underlying DateTime object.
Use with caution if mutability is a concern elsewhere.
no setter
- day → Day
-
Gets the Day part of this time instance.
no setter
- endOfDay → Time
-
Returns a new Time instance representing the very end of the day (23:59:59.999999).
no setter
- hashCode → int
-
Returns the hash code for this time instance.
no setteroverride
- hour → int
-
Gets the hour of the day (0-23).
no setter
- isAfterNow → bool
-
Checks if this time instance is after the current moment.
no setter
- isBeforeNow → bool
-
Checks if this time instance is before the current moment.
no setter
- isToday → bool
-
Checks if this time instance falls on the current date.
no setter
- isTomorrow → bool
-
Checks if this time instance falls on the date after the current date.
no setter
- isYesterday → bool
-
Checks if this time instance falls on the date before the current date.
no setter
- microsecond → int
-
Gets the microsecond of the second (0-999).
no setter
- microsecondsSinceEpoch → int
-
Gets the number of microseconds since the Unix epoch (January 1, 1970, UTC).
no setter
- millisecond → int
-
Gets the millisecond of the second (0-999).
no setter
- millisecondsSinceEpoch → int
-
Gets the number of milliseconds since the Unix epoch (January 1, 1970, UTC).
no setter
- minute → int
-
Gets the minute of the hour (0-59).
no setter
- month → Month
-
Gets the Month part of this time instance.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- second → int
-
Gets the second of the minute (0-59).
no setter
- startOfDay → Time
-
Returns a new Time instance representing the start of the day (00:00:00.000000).
no setter
- weekday → Weekday
-
Gets the Weekday part of this time instance.
no setter
- year → Year
-
Gets the Year part of this time instance.
no setter
Methods
-
add(
Duration duration) → Time -
Returns a new Time instance with the given
duration
added. -
addDays(
int days) → Time -
Returns a new Time instance with the specified number of
days
added. -
addHours(
int hours) → Time -
Returns a new Time instance with the specified number of
hours
added. -
addMinutes(
int minutes) → Time -
Returns a new Time instance with the specified number of
minutes
added. -
addSeconds(
int seconds) → Time -
Returns a new Time instance with the specified number of
seconds
added. -
compareTo(
Time other) → int -
Compares this time instance to
other
. Returns < 0 if this is earlier, 0 if same moment, > 0 if this is later.override -
copyWith(
{int? year, int? month, int? day, int? hour, int? minute, int? second, int? millisecond, int? microsecond}) → Time - Creates a copy of this Time instance but with the given fields updated.
-
dayDifference(
Time other) → int -
Returns the difference in whole days between this time and
other
. -
difference(
Time other) → Duration -
Returns the Duration between this time instance and
other
. The duration is positive if this time is later thanother
. -
differenceFromNow(
) → Duration - Returns the Duration between this time instance and the current moment.
-
format(
String formatPattern, [bool utc = false]) → String - Formats this time instance using the provided format string.
-
format12h(
) → String -
Formats the time using the 12-hour clock with AM/PM (e.g., "01:52 PM").
TimeFormat.hhmma
provides the pattern 'hh:mm a'. -
format12hUtc(
) → String - Formats the time in UTC using the 12-hour clock with AM/PM.
-
format24h(
) → String -
Formats the time using the 24-hour clock with seconds (e.g., "13:52:05").
TimeFormat.HHmmss
provides the pattern 'HH:mm:ss'. -
format24hUtc(
) → String - Formats the time in UTC using the 24-hour clock with seconds.
-
formatDate(
) → String -
Formats the date part as YYYY-MM-DD (e.g., "2025-04-09").
TimeFormat.yyyyMMdd
provides the pattern 'yyyy-MM-dd'. -
formatReadableDate(
) → String -
Formats the date part readably (e.g., "09 April 2025").
TimeFormat.ddMMMMyyyy
provides the pattern 'dd MMMM yyyy'. -
formatReadableTime(
) → String -
Formats the date and time readably (e.g., "Wed, 09 Apr 2025 12:11:00 AM").
TimeFormat.EEEddMMMyyyyhhmmssa
provides 'EEE, dd MMM yyyy hh:mm:ss a'. -
formatTime(
) → String -
Formats the date and time (e.g., "2025-04-09 00:11:00").
TimeFormat.yyyyMMddHHmmss
provides the pattern 'yyyy-MM-dd HH:mm:ss'. -
hourDifference(
Time other) → int -
Returns the difference in whole hours between this time and
other
. -
isAfter(
Time other) → bool -
Checks if this time instance is chronologically after
other
. -
isAtSameMomentAs(
Time other) → bool -
Checks if this time instance represents the exact same moment as
other
. -
isBefore(
Time other) → bool -
Checks if this time instance is chronologically before
other
. -
isInRange(
Time start, Time end, {bool inclusiveStart = true, bool inclusiveEnd = true}) → bool - Checks if this time instance falls within the specified range.
-
isSameDay(
Time other) → bool -
Checks if this Time instance represents the same calendar day as
other
. Ignores the time part. -
isSameMonth(
Time other) → bool -
Checks if this Time instance is in the same calendar month as
other
. Ignores day and time parts. -
isSameYear(
Time other) → bool -
Checks if this Time instance is in the same calendar year as
other
. -
minuteDifference(
Time other) → int -
Returns the difference in whole minutes between this time and
other
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
secondDifference(
Time other) → int -
Returns the difference in whole seconds between this time and
other
. -
subtract(
Duration duration) → Time -
Returns a new Time instance with the given
duration
subtracted. -
subtractDays(
int days) → Time -
Returns a new Time instance with the specified number of
days
subtracted. -
subtractHours(
int hours) → Time -
Returns a new Time instance with the specified number of
hours
subtracted. -
subtractMinutes(
int minutes) → Time -
Returns a new Time instance with the specified number of
minutes
subtracted. -
subtractSeconds(
int seconds) → Time -
Returns a new Time instance with the specified number of
seconds
subtracted. -
toIso8601String(
) → String - Returns an ISO 8601 representation of this time instance.
-
toLocal(
) → Time - Returns a new Time instance representing the same moment in the local time zone.
-
toString(
) → String -
Returns a string representation of the underlying DateTime object.
override
-
toUtc(
) → Time - Returns a new Time instance representing the same moment in UTC.
-
toUtcString(
) → String - Returns a string representation of the UTC equivalent of this time instance.
Operators
-
operator ==(
Object other) → bool -
Returns true if this Time instance represents the same moment as
other
.override