LocalTime class final

The time of the day without a timezone, as seen on a wall clock, such as. 12:30.

A LocalTime is immutable. It cannot represent a specific point in time without an additional offset or timezone. Time is stored to microsecond precision See range for the valid range of LocalTimes.

Working with LocalTimes

To create a LocalTime:

final now = LocalTime.now();
final moonLanding = LocalTime(18, 4);

You can add and subtract different types of time intervals, including:

LocalTime behaves the same for all 3 types of time intervals. All calculations wrap around midnight.

final later = now.add(const Duration(hours: 1);
final evenLater = now + const Period(hour: 2);
final latest = now.plus(hours: 3);

LocalTimes can be compared using the comparison operators such as <.

print(now < later); // true
print(latest >= evenLater); // true

You can also truncate, round, ceil and floor LocalTime.

print(moonLanding.truncate(to: TimeUnit.hours); // 18:00
print(moonLanding.round(TimeUnit.minutes, 5);   // 18:05
print(moonLanding.ceil(TimeUnit.minutes, 5);    // 18:05
print(moonLanding.floor(TimeUnit.minutes, 5);   // 18:00

Testing

LocalTime.now can be stubbed by setting System.currentDateTime:

void main() {
  test('mock LocalDateTime.now()', () {
    System.currentDateTime = () => DateTime.utc(2023, 7, 9, 23, 30);
    expect(LocalTime.now(), LocalTime(23, 30));
  });
}

Other resources

See OffsetTime to represent times with offsets.

Mixed-in types

Constructors

LocalTime([int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0])
Creates a LocalTime.
LocalTime.fromDayMicroseconds(DayMicroseconds microseconds)
Creates a LocalTime with the microseconds since midnight, wrapping around midnight.
LocalTime.fromDayMilliseconds(DayMilliseconds milliseconds)
Creates a LocalTime from the milliseconds since midnight, wrapping around midnight.
LocalTime.now([TimeUnit precision = TimeUnit.microseconds])
Creates a LocalTime that represents the current time.
factory

Properties

dayMicroseconds → DayMicroseconds
The microseconds since midnight.
no setter
dayMilliseconds → DayMicroseconds
The milliseconds since midnight.
no setter
hashCode → DayMicroseconds
The hash code for this object.
no setterinherited
hashValue → DayMicroseconds
This object's hash.
no setteroverride
hour → DayMicroseconds
The hour.
no setterinherited
microsecond → DayMicroseconds
The microsecond.
no setterinherited
millisecond → DayMicroseconds
The millisecond.
no setterinherited
minute → DayMicroseconds
The minute.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
second → DayMicroseconds
The second.
no setterinherited

Methods

add(Duration duration) → LocalTime
Returns a copy of this with the duration added, wrapping around midnight.
at(Offset offset) → OffsetTime
Converts this to a OffsetTime.
ceil(TimeUnit unit, int value) → LocalTime
Returns a copy of this ceiled to the nearest unit and value.
compareTo(LocalTime other) → int
Compares this object to another object.
override
copyWith({int? hour, int? minute, int? second, int? millisecond, int? microsecond}) → LocalTime
Returns a copy of this with the updated units of time.
difference(LocalTime other) → Duration
Returns the difference between this and other.
floor(TimeUnit unit, int value) → LocalTime
Returns a copy of this floored to the nearest unit and value.
minus({int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0, int microseconds = 0}) → LocalTime
Returns a copy of this with the time units subtracted, wrapping around midnight.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
plus({int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0, int microseconds = 0}) → LocalTime
Returns a copy of this with the time units added, wrapping around midnight.
round(TimeUnit unit, int value) → LocalTime
Returns a copy of this rounded to the nearest unit and value.
subtract(Duration duration) → LocalTime
Returns a copy of this with the duration subtracted, wrapping around midnight.
toNative() → DateTime
Converts this to a DateTime in UTC.
toString() → String
Returns a ISO formatted string representation.
override
truncate({required TimeUnit to}) → LocalTime
Returns a copy of this truncated to the TimeUnit.

Operators

operator +(Period period) → LocalTime
Returns a copy of this with the period added.
operator -(Period period) → LocalTime
Returns a copy of this with the period subtracted.
operator <(LocalTime other) → bool
Returns true if this is less than other.
inherited
operator <=(LocalTime other) → bool
Returns true if this is equal to or less than other.
inherited
operator ==(Object other) → bool
The equality operator.
inherited
operator >(LocalTime other) → bool
Returns true if this is more than other.
inherited
operator >=(LocalTime other) → bool
Returns true if this is equal to or more than other.
inherited

Static Properties

midnight → LocalTime
Midnight, 00:00.
final
noon → LocalTime
Noon, 12:00.
final
range → Interval<LocalTime>
The valid range of LocalTimes, from 00:00 to 23:59:59.999999, inclusive.
final