LocalDate
Represents a date without a time-zone in the ISO-8601 calendar system. This is an abstract class and cannot be instantiated directly.
Properties
dayOfMonth
dayOfMonth:
number
The day of the month, from 1 to 31.
Example
const date = LocalDate.of(2023, 9, 15);
print(date.dayOfMonth); // Output: 15
dayOfWeek
dayOfWeek:
number
The day of the week, from 1 (Monday) to 7 (Sunday).
Example
const date = LocalDate.of(2023, 9, 15); // September 15, 2023 is a Friday
print(date.dayOfWeek); // Output: 5
dayOfYear
dayOfYear:
number
The day of the year, from 1 to 365 (or 366 in a leap year).
Example
const date = LocalDate.of(2023, 9, 15);
print(date.dayOfYear); // Output: 258 (as September 15 is the 258th day of 2023)
month
month:
number
The month of the year, from 1 (January) to 12 (December).
Example
const date = LocalDate.of(2023, 9, 15);
print(date.month); // Output: 9
year
year:
number
The year.
Example
const date = LocalDate.of(2023, 9, 15);
print(date.year); // Output: 2023
Methods
add()
add(
duration):LocalDate
Adds a specified duration to this date.
Parameters
duration
The duration to add.
Returns
LocalDate
A new LocalDate representing the result.
Example
// Assuming we have a LocalDate instance 'date' and a Duration 'duration'
const newDate = date.add(duration);
compareTo()
compareTo(
other):number
Compares this date to another.
Parameters
other
LocalDate
The date to compare to.
Returns
number
A negative integer, zero, or a positive integer if this date is before, equal to, or after the given date.
Example
const date1 = LocalDate.of(2023, 1, 1);
const date2 = LocalDate.of(2023, 1, 2);
print(date1.compareTo(date2)); // Output: -1
isAfter()
isAfter(
other):boolean
Checks if this date is after the specified date.
Parameters
other
LocalDate
The date to compare to.
Returns
boolean
True if this date is after the specified date.
Example
const date1 = LocalDate.of(2023, 1, 2);
const date2 = LocalDate.of(2023, 1, 1);
print(date1.isAfter(date2)); // Output: true
isBefore()
isBefore(
other):boolean
Checks if this date is before the specified date.
Parameters
other
LocalDate
The date to compare to.
Returns
boolean
True if this date is before the specified date.
Example
const date1 = LocalDate.of(2023, 1, 1);
const date2 = LocalDate.of(2023, 1, 2);
print(date1.isBefore(date2)); // Output: true
isEqual()
isEqual(
other):boolean
Checks if this date is equal to the specified date.
Parameters
other
LocalDate
The date to compare to.
Returns
boolean
True if the dates are equal.
Example
const date1 = LocalDate.of(2023, 1, 1);
const date2 = LocalDate.of(2023, 1, 1);
print(date1.isEqual(date2)); // Output: true
minus()
minus(
duration):LocalDate
Returns a copy of this date minus the specified duration.
Parameters
duration
The duration to subtract.
Returns
LocalDate
A new LocalDate representing the result.
Example
// Assuming we have a LocalDate instance 'date' and a Duration 'duration'
const newDate = date.minus(duration);
minusDays()
minusDays(
days):LocalDate
Returns a copy of this date minus the specified number of days.
Parameters
days
number
The number of days to subtract.
Returns
LocalDate
A new LocalDate representing the result.
Example
const date = LocalDate.of(2023, 1, 10);
const newDate = date.minusDays(5);
print(newDate.toString()); // Output: "2023-01-05"
minusMonths()
minusMonths(
months):LocalDate
Returns a copy of this date minus the specified number of months.
Parameters
months
number
The number of months to subtract.
Returns
LocalDate
A new LocalDate representing the result.
Example
const date = LocalDate.of(2023, 3, 15);
const newDate = date.minusMonths(2);
print(newDate.toString()); // Output: "2023-01-15"
minusWeeks()
minusWeeks(
weeks):LocalDate
Returns a copy of this date minus the specified number of weeks.
Parameters
weeks
number
The number of weeks to subtract.
Returns
LocalDate
A new LocalDate representing the result.
Example
const date = LocalDate.of(2023, 1, 15);
const newDate = date.minusWeeks(2);
print(newDate.toString()); // Output: "2023-01-01"
minusYears()
minusYears(
years):LocalDate
Returns a copy of this date minus the specified number of years.
Parameters
years
number
The number of years to subtract.
Returns
LocalDate
A new LocalDate representing the result.
Example
const date = LocalDate.of(2023, 1, 1);
const newDate = date.minusYears(3);
print(newDate.toString()); // Output: "2020-01-01"
plus()
plus(
duration):LocalDate
Returns a copy of this date plus the specified duration.
Parameters
duration
The duration to add.
Returns
LocalDate
A new LocalDate representing the result.
Example
// Assuming we have a LocalDate instance 'date' and a Duration 'duration'
const newDate = date.plus(duration);
plusDays()
plusDays(
days):LocalDate
Returns a copy of this date plus the specified number of days.
Parameters
days
number
The number of days to add.
Returns
LocalDate
A new LocalDate representing the result.
Example
const date = LocalDate.of(2023, 1, 1);
const newDate = date.plusDays(5);
print(newDate.toString()); // Output: "2023-01-06"
plusMonths()
plusMonths(
months):LocalDate
Returns a copy of this date plus the specified number of months.
Parameters
months
number
The number of months to add.
Returns
LocalDate
A new LocalDate representing the result.
Example
const date = LocalDate.of(2023, 1, 15);
const newDate = date.plusMonths(2);
print(newDate.toString()); // Output: "2023-03-15"
plusWeeks()
plusWeeks(
weeks):LocalDate
Returns a copy of this date plus the specified number of weeks.
Parameters
weeks
number
The number of weeks to add.
Returns
LocalDate
A new LocalDate representing the result.
Example
const date = LocalDate.of(2023, 1, 1);
const newDate = date.plusWeeks(2);
print(newDate.toString()); // Output: "2023-01-15"
plusYears()
plusYears(
years):LocalDate
Returns a copy of this date plus the specified number of years.
Parameters
years
number
The number of years to add.
Returns
LocalDate
A new LocalDate representing the result.
Example
const date = LocalDate.of(2023, 1, 1);
const newDate = date.plusYears(3);
print(newDate.toString()); // Output: "2026-01-01"
toString()
toString(
format?):string
Converts this date to a string representation.
Parameters
format?
string
The format to use for the string representation.
Returns
string
A string representation of this date.
Example
const date = LocalDate.of(2023, 1, 15);
print(date.toString()); // Output: "2023-01-15"
print(date.toString("dd/MM/uuuu")); // Output: "15/01/2023"
withDayOfMonth()
withDayOfMonth(
day):LocalDate
Returns a copy of this date with the day of month altered.