LocalDateTime class

A date-time without a timezone, such as 2023-04-10 09:30.

A LocalDateTime is immutable. It cannot represent a specific point in time without an additional offset or timezone. Time is stored to microsecond precision.

Working with LocalDateTimes

To create a LocalDateTime:

final now = LocalDateTime.now();
final moonLanding = LocalDateTime(1969, 7, 20, 18, 4);

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

LocalDateTime behaves the same for all 3 types of time intervals.

final tomorrow = now.add(const Duration(days: 1);
final dayAfterTomorrow = now + const Period(days: 2);
final threeDaysAfter = now.plus(days: 3);

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

print(now < tomorrow); // true
print(dayAfterTomorrow >= now); // true

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

print(moonLanding.truncate(to: TimeUnit.hours); // 1969-07-20 18:00
print(moonLanding.round(DateUnit.days, 7);      // 1969-07-21 00:00
print(moonLanding.ceil(DateUnit.days, 7);       // 1969-07-21 00:00
print(moonLanding.floor(DateUnit.days, 7);      // 1969-07-14 00:00

Other resources

See ZonedDateTime to represent date and times with timezones.

Mixed-in types

Constructors

LocalDateTime(int year, [int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0])
Creates a LocalDateTime.
LocalDateTime.fromEpochMicroseconds(EpochMicroseconds microseconds)
Creates a LocalDateTime from the milliseconds since Unix epoch (January 1st 1970).
LocalDateTime.fromEpochMilliseconds(EpochMilliseconds milliseconds)
Creates a LocalDateTime from the milliseconds since Unix epoch (January 1st 1970).
LocalDateTime.now()
Creates a LocalDateTime that represents the current date-time.

Properties

date LocalDate
The date.
no setter
day int
The day.
no setterinherited
dayOfYear int
The ordinal day of the year.
no setter
daysInMonth int
The number of days in the month.
no setter
epochMicroseconds EpochMicroseconds
The microseconds since Unix epoch, assuming this datetime is in UTC.
no setter
epochMilliseconds EpochMilliseconds
The milliseconds since Unix epoch, assuming this datetime is in UTC.
no setter
firstDayOfMonth LocalDateTime
The first day of the month.
no setter
firstDayOfWeek LocalDateTime
The first day of the week.
no setter
hashCode int
The hash code for this object.
no setterinherited
hashValue int
This object's hash.
no setteroverride
hour int
The hour.
no setterinherited
lastDayOfMonth LocalDateTime
The last day of the month.
no setter
lastDayOfWeek LocalDateTime
The last day of the week.
no setter
leapYear bool
Whether this year is a leap year.
no setter
microsecond int
The microsecond.
no setterinherited
millisecond int
The millisecond.
no setterinherited
minute int
The minute.
no setterinherited
month int
The month.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
second int
The second.
no setterinherited
time LocalTime
The time.
no setter
weekday int
The day of the week.
no setter
weekOfYear int
The ordinal week of the year.
no setter
year int
The year.
no setterinherited

Methods

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

Operators

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