ts-time/LocalDate

Consumption

import LocalDate, {EPOCH_DATE} from "ts-time/LocalDate";

Default export#

Hierarchy#

  • class LocalDate

Description#

Comprises a specific date, merely as a tuple of (absolute year, month, day of month).

Construct#

A common way to construct an object in ts-time is to call its of static method. For example, the following LocalDate instances represent 15th of February, 2022:

LocalDate.of(2022, FEBRUARY, 15);
LocalDate.of(2022, 2, 15);

As opposed to Java API, there's no common way to get the current instance of an object in ts-time, because ts-time doesn't have a concept of default time zone. Instead, you must get the current Instant and convert it to an object that you need, taking time zone into consideration.

Instant.now().atZone(UTC).date;             // Current LocalDate in UTC
Instant.now().atZone(LOCAL_ZONE_ID).date;   // Current LocalDate in the local time zone
Instant.now().atZone(zone).date;            // Current LocalDate in a given ZoneId/ZoneOffset

It makes ts-time API more robust. It leaves less room for a mistake.

A common way to convert a native JavaScript Date object to a ts-time object is to call fromNative* static method:

LocalDate.fromNativeUtc(date);              // LocalDate from a given Date in UTC
LocalDate.fromNativeLocal(date);            // LocalDate from a given Date in the local time zone
Instant.fromNative(date).atZone(zone).date; // LocalDate from a given Date in a given ZoneId/ZoneOffset

You can convert instances of other ts-time classes to LocalDate via their properties and methods:

instant.atZone(zone).date;                  // From Instant in a given ZoneId/ZoneOffset
localDateTime.date;                         // From LocalDateTime
offsetDateTime.date;                        // From OffsetDateTime in the same ZoneOffset
zonedDateTime.date;                         // From ZonedDateTime in the same ZoneId

Please notice that all these conversions result in a partial data loss (time, zone, offset).

Other sophisticated constructors: ofEpochDay, ofYearDay, ofWeek, ofWeekBasedYearDay.

Parse#

A common way to parse an ISO 8601 compliant string in ts-time is to call parse static method. For example, the following LocalDate instance represents 15th of February, 2022:

LocalDate.parse("2022-02-15");

The library doesn't yet support parsing non-compliant strings.

Inspect#

In the following example, we inspect various properties of a LocalDate object. Please notice the difference in return value types:

const date           = LocalDate.of(2022, FEBRUARY, 15);
const year           = date.year;            // 2022
const month          = date.month;           // FEBRUARY
const monthValue     = date.month.value;     // 2
const dayOfMonth     = date.dayOfMonth;      // 15
const dayOfWeek      = date.dayOfWeek;       // TUESDAY
const dayOfWeekValue = date.dayOfWeek.value; // 2

Other sophisticated features for LocalDate inspection: era, yearOfEra, weekBasedYear, weekOfWeekBasedYear, dayOfYear, dayOfWeekBasedYear, epochDay, quarterOfYear, isLeapYear, lengthOfYear.

Compare#

A common way to compare objects in ts-time is to call equals, isBefore, isAfter, compareTo methods:

const d1 = LocalDate.of(2022, FEBRUARY, 15);
const d2 = LocalDate.of(2022, FEBRUARY, 16);
d1.equals(d2);    // false
d1.isBefore(d2);  // true
d1.isAfter(d2);   // false
d1.compareTo(d2); // < 0

For nullable objects, use static methods instead. Null and undefined are considered less than anything, except each other:

const d1: LocalDate = null;
const d2 = LocalDate.of(2022, FEBRUARY, 16);
LocalDate.equal(d1, d2);     // false
LocalDate.isBefore(d1, d2);  // true
LocalDate.isAfter(d1, d2);   // false
LocalDate.compare(d1, d2);   // < 0
Manipulate#

Every object in ts-time is immutable. Therefore every manipulation returns a new object.

To add/subtract a Period, call plus/minus method:

const date = LocalDate.of(2022, FEBRUARY, 15);
const d1 = date.plus(DAY_PERIOD);       // 16th of February, 2022
const d2 = date.plus(Period.ofDays(2)); // 17th of February, 2022
const d3 = date.minus(MONTH_PERIOD);    // 15th of January, 2022

To change one of the components, preserving all the rest, call with* methods:

const date = LocalDate.of(2022, FEBRUARY, 15);
const d1 = date.withYear(2025);         // 15th of February, 2025
const d2 = date.withMonth(APRIL);       // 15th of April, 2022
const d3 = date.withDayOfMonth(10);     // 10th of February, 2022
const d4 = date.withDayOfWeek(SUNDAY);  // 20th of February, 2022, Sunday

To truncate all the least-significant components, call truncate* methods:

const date = LocalDate.of(2022, FEBRUARY, 15);
const d1 = date.truncateToYear;         // 1st of January, 2022
const d2 = date.truncateToMonth;        // 1st of February, 2022
const d3 = date.truncateToWeek;         // 14th of February, 2022, Monday

The library doesn't yet support Sunday as the 1st day of the week. But there's a workaround:

const d4 = date.minus(Period.ofDays(date.dayOfWeek.value % 7)); // 13th of February, 2022, Sunday

Or a flexible solution, where firstDayOfWeek is 1 for Monday, and 7 for Sunday:

const d5 = date.minus(Period.ofDays((date.dayOfWeek.value + 7 - firstDayOfWeek) % 7));

Other sophisticated features for LocalDate manipulation: withDayOfYear, truncateToWeekBasedYear.

Convert#

You can convert LocalDate to other kinds of objects via its properties and methods:

const date = LocalDate.of(2022, FEBRUARY, 15);
date.atStartOfDay;                      // To LocalDateTime at midnight
date.atTime(MIDNIGHT);                  // Equivalent
date.atTime(time);                      // To LocalDateTime at a given LocalTime
date.atTime(time).atOffset(offset);     // To OffsetDateTime at a given LocalTime in a given ZoneOffset
date.atTime(time).atZone(zone);         // To ZonedDateTime at a given LocalTime in a given ZoneId
date.atTime(time).atZone(zone).instant; // To Instant at a given LocalTime in a given ZoneId/ZoneOffset

You can as well convert LocalDate to a native JavaScript Date:

const date = LocalDate.of(2022, FEBRUARY, 15);
date.nativeUtc;                         // Date representing 15th of February, 2022, midnight in UTC
date.nativeLocal;                       // Date representing 15th of February, 2022, midnight in the local time zone
date.atStartOfDay.atZone(zone).native;  // Date representing 15th of February, 2022, midnight in a given ZoneId/ZoneOffset
date.atTime(time).atZone(zone).native;  // Date representing 15th of February, 2022, at a given LocalTime in a given ZoneId/ZoneOffset

For backward conversion, see Construct.

Format#

Every class in ts-time has ISO 8601 compliant toString method:

const date = LocalDate.of(2022, FEBRUARY, 15);
date.toString(); // "2022-02-15"

For more sophisticated string formatting, add ts-time-format library to your list of dependencies:

npm install --save ts-time-format

Now you can construct an instance of DateFormatter in order to format arbitrary LocalDate instances:

const formatter = DateFormatter.ofPattern("dd.MMM''yy");
const date = LocalDate.of(2022, FEBRUARY, 15);
formatter.format(date); // "15.Feb'22"

You can define a custom context object to internationalize the formatted strings:

const context = {monthShortNames: ["Янв", "Фев", "Мар"]};
formatter.format(date, context); // "15.Фев'22"

Fields#

nativeLocal#

readonly nativeLocal: Date

Native JS Date representing this local date in the browser time zone at MIDNIGHT, for compatibility with native and third party API.
nativeUtc#

readonly nativeUtc: Date

Native JS Date representing this local date in UTC at MIDNIGHT, for compatibility with native and third party API.
era#

readonly era: Era

Era of this local date.
year#

readonly year: number

Absolute year.
yearOfEra#

readonly yearOfEra: number

Year of era. See Era for details.
weekBasedYear#

readonly weekBasedYear: number

Absolute week based year.

By definition, the 1st week of week based year contains the 1st Thursday of the year, and the week based year starts from the Monday of this week.

month#

readonly month: Month

Month.
weekOfWeekBasedYear#

readonly weekOfWeekBasedYear: number

1-based week of week-based year.

By definition, the 1st week of week based year contains the 1st Thursday of the year, and the week based year starts from the Monday of this week.

dayOfYear#

readonly dayOfYear: number

1-based day of year.
dayOfWeekBasedYear#

readonly dayOfWeekBasedYear: number

1-based day of week-based year.

By definition, the 1st week of week based year contains the 1st Thursday of the year, and the week based year starts from the Monday of this week.

dayOfMonth#

readonly dayOfMonth: number

1-based day of month.
dayOfWeek#

readonly dayOfWeek: DayOfWeek

Day of week.
epochDay#

readonly epochDay: number

1-based day since epoch (i.e. 1st of JANUARY 1970 is the 1st epoch day).
quarterOfYear#

readonly quarterOfYear: number

1-based quarter of year.
isLeapYear#

readonly isLeapYear: boolean

True if this is a leap year.
lengthOfYear#

readonly lengthOfYear: number

Number of days in this year (365 for non-leap, 366 for leap).
atStartOfDay#

readonly atStartOfDay: LocalDateTime

Start of the day (i.e. MIDNIGHT).
truncateToYear#

readonly truncateToYear: LocalDate

Start of the year (i.e. the 1st of JANUARY).
truncateToWeekBasedYear#

readonly truncateToWeekBasedYear: LocalDate

Start of the week-based year (i.e. MONDAY, MIDNIGHT).

By definition, the 1st week of week based year contains the 1st Thursday of the year, and the week based year starts from the Monday of this week.

truncateToMonth#

readonly truncateToMonth: LocalDate

Start of the month (i.e. 1st day of one).
truncateToWeek#

readonly truncateToWeek: LocalDate

Start of the week (i.e. Monday).

Methods#

compareTo#

(other: LocalDate): number

other
Local date to compare to.
returns
  • 0 if this is the same local date;
  • positive value if this local date goes after other;
  • negative value if this local date goes before other.
Null and undefined are considered less than anything, except each other.

Note that a method call on null or undefined always leads to an error. So, if your variable may contain null or undefined, use the respective static method instead.

equals#

(other: LocalDate): boolean

other
Local date to compare to.
returns
True if this local date is equal to other. Null and undefined are only equal to each other.

Note that a method call on null or undefined always leads to an error. So, if your variable may contain null or undefined, use the respective static method instead.

isBefore#

(other: LocalDate): boolean

other
Local date to compare to.
returns
True if this local date goes before other. Null and undefined go before anything, except each other.

Note that a method call on null or undefined always leads to an error. So, if your variable may contain null or undefined, use the respective static method instead.

isAfter#

(other: LocalDate): boolean

other
Local date to compare to.
returns
True if this local date goes after other. Null and undefined go before anything, except each other.

Note that a method call on null or undefined always leads to an error. So, if your variable may contain null or undefined, use the respective static method instead.

atTime#

(time: LocalTime): LocalDateTime

time
Local time.
returns
Local date/time at the specified time.
plus#

(period: Period): LocalDate

period
Period to add.
returns
New LocalDate shifted onwards from this one by the specified period.
minus#

(period: Period): LocalDate

period
Period to subtract.
returns
New LocalDate shifted backwards from this one by the specified period.
withYear#

(year: number): LocalDate

year
Absolute year.
returns
New LocalDate with the specified year and its current month/day of month. If such date doesn't exist, it shifts the date back to the nearest valid one (e.g. 29th of FEBRUARY in 2019 gets shifted to 28th of FEBRUARY).
withMonth#

(month: number | Month): LocalDate

month
Month.
returns
New LocalDate with the specified month and its current year/day of month. If such date doesn't exist, it shifts the date back to the nearest valid one (e.g. 31st of APRIL gets shifted to 30th of APRIL).
withDayOfMonth#

(dayOfMonth: number): LocalDate

dayOfMonth
1-based day of month.
returns
New LocalDate with the specified day of month and its current year/month.
withDayOfWeek#

(dayOfWeek: number | DayOfWeek): LocalDate

dayOfWeek
Day of week or its numeric value
returns
New LocalDate with the specified day of week and its current week.
withDayOfYear#

(dayOfYear: number): LocalDate

dayOfYear
1-based day of year.
returns
New LocalDate with the specified day of year and its current year.
toString#

(): string

returns
String representation of the date in ISO format, such as "2019-12-30".

Static methods#

of#

(year: number, month: number | Month = JANUARY, dayOfMonth: number = 1): LocalDate

year
Absolute year.
month
Month or its numeric value.
dayOfMonth
1-based day of month.
returns
LocalDate representation.
ofEpochDay#

(epochDay: number): LocalDate

epochDay
1-based epoch day (i.e. 1st of JANUARY 1970 is the 1st epoch day).
returns
LocalDate representation.
ofYearDay#

(year: number, dayOfYear: number): LocalDate

year
Absolute year.
dayOfYear
1-based day of year.
returns
LocalDate representation.
ofWeek#

(year: number, week: number, dayOfWeek: number | DayOfWeek): LocalDate

year
Absolute week based year.
week
1-based week of the year.
dayOfWeek
Day of week or its numeric value.
returns
LocalDate representation.

By definition, the 1st week of week based year contains the 1st Thursday of the year, and the week based year starts from the Monday of this week.

ofWeekBasedYearDay#

(year: number, dayOfWeekBasedYear: number): LocalDate

year
Absolute week based year.
dayOfWeekBasedYear
1-based day of the week based year.
returns
LocalDate representation.

By definition, the 1st week of week based year contains the 1st Thursday of the year, and the week based year starts from the Monday of this week.

fromNativeLocal#

(date: Date): LocalDate

date
Native JS date.
returns
LocalDate representation of date at MIDNIGHT in the browser time zone, for compatibility with native and third party API.
fromNativeUtc#

(date: Date): LocalDate

date
Native JS date.
returns
LocalDate representation of date at MIDNIGHT in UTC, for compatibility with native and third party API.
parse#

(str: string): LocalDate throws TemporalParsingError

str
String representation of a local date in ISO format, such as '2019-12-30'.
returns
LocalDate representation.
compare#

(x: LocalDate, y: LocalDate): number

x
One local date.
y
Another local date.
returns
  • 0 if this x and y are the same local dates;
  • positive value if x goes after y;
  • negative value if x goes before y.
Null and undefined are considered less than anything, except each other.
equal#

(x: LocalDate, y: LocalDate): boolean

x
One local date.
y
Another local date.
returns
True if x is equal to y. Null and undefined are only equal to each other.
isBefore#

(x: LocalDate, y: LocalDate): boolean

x
One local date.
y
Another local date.
returns
True if x goes before y. Null and undefined go before anything, except each other.
isAfter#

(x: LocalDate, y: LocalDate): boolean

x
One local date.
y
Another local date.
returns
True if x goes after y. Null and undefined go before anything, except each other.

EPOCH_DATE#

EPOCH_DATE: LocalDate

LocalDate with epochDay = 1, i.e. representing ECMAScript epoch (1st of JANUARY 1970).