LocalDate class
A date without a timezone, as seen in a calendar, such as 2023-04-11
.
A LocalDate
is immutable. It cannot represent a specific point in time without an additional offset or timezone.
Working with LocalDate
s
To create a LocalDate
:
final today = LocalDate.now();
final moonLanding = LocalDate(1969, 7, 20);
You can add and subtract different types of time intervals, including:
LocalDate
behaves the same for all 3 types of time intervals.
final tomorrow = today.add(const Duration(days: 1);
final dayAfterTomorrow = today + const Period(days: 2);
final threeDaysAfter = today.plus(days: 3);
LocalDate
s can be compared using the comparison operators such as <.
print(today < tomorrow); // true
print(dayAfterTomorrow >= today); // true
You can also truncate, round, ceil and floor LocalDate
.
print(moonLanding.truncate(to: DateUnit.months); // 1969-07-01
print(moonLanding.round(DateUnit.days, 7); // 1969-07-21
print(moonLanding.ceil(DateUnit.days, 7); // 1969-07-21
print(moonLanding.floor(DateUnit.days, 7); // 1969-07-14
Other resources
See:
- LocalDateTime to represent date -times without timezones.
- ZonedDateTime to represent date -times with timezones.
Constructors
- LocalDate(int year, [int month = 1, int day = 1])
- Creates a LocalDate.
- LocalDate.fromEpochDays(EpochDays days)
-
Creates a LocalDate with the
days
since Unix epoch (January 1st 1970). - LocalDate.fromEpochMicroseconds(EpochMicroseconds microseconds)
-
Creates a LocalDate with the
microseconds
since Unix epoch (January 1st 1970), floored to the nearest day. - LocalDate.fromEpochMilliseconds(EpochMilliseconds milliseconds)
-
Creates a LocalDate with the
milliseconds
since Unix epoch (January 1st 1970), floored to the nearest day. - LocalDate.now()
-
Creates a LocalDate that represents the current date.
factory
Properties
- 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
- epochDays → int
-
The days since Unix epoch, assuming this date is in UTC.
no setter
- epochMicroseconds → int
-
The microseconds since Unix epoch, assuming this date is in UTC.
no setter
- epochMilliseconds → int
-
The milliseconds since Unix epoch, assuming this date is in UTC.
no setter
- firstDayOfMonth → LocalDate
-
The first day of the month.
no setter
- firstDayOfWeek → LocalDate
-
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
- lastDayOfMonth → LocalDate
-
The last day of the month.
no setter
- lastDayOfWeek → LocalDate
-
The last day of the week.
no setter
- leapYear → bool
-
Whether this year is a leap year.
no setter
- month → int
-
The month.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- 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) → LocalDate -
Returns a copy of this with the
duration
added. -
at(
LocalTime time) → LocalDateTime - Returns a LocalDateTime on this date at the given time.
-
ceil(
DateUnit unit, int value) → LocalDate -
Returns a copy of this ceiled to the nearest
unit
andvalue
. -
compareTo(
LocalDate other) → int -
Compares this object to another object.
override
-
copyWith(
{int? year, int? month, int? day}) → LocalDate - Returns a copy of this with the updated units of time.
-
difference(
LocalDate other) → Duration -
Returns the difference between this and
other
. -
floor(
DateUnit unit, int value) → LocalDate -
Returns a copy of this floored to the nearest
unit
andvalue
. -
minus(
{int years = 0, int months = 0, int days = 0}) → LocalDate - 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}) → LocalDate - Returns a copy of this with the units of time added.
-
round(
DateUnit unit, int value) → LocalDate -
Returns a copy of this rounded to the nearest
unit
andvalue
. -
subtract(
Duration duration) → LocalDate -
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 DateUnit to}) → LocalDate - Returns a copy of this truncated to the given DateUnit.
Operators
-
operator +(
Period period) → LocalDate -
Returns a copy of this with the
period
added. -
operator -(
Period period) → LocalDate -
Returns a copy of this with the
period
subtracted. -
operator <(
LocalDate other) → bool -
Returns true if this is less than
other
.inherited -
operator <=(
LocalDate other) → bool -
Returns true if this is equal to or less than
other
.inherited -
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator >(
LocalDate other) → bool -
Returns true if this is more than
other
.inherited -
operator >=(
LocalDate other) → bool -
Returns
true
if this is equal to or more thanother
.inherited