import LocalDate, {EPOCH_DATE} from "ts-time/LocalDate";
Comprises a specific date, merely as a tuple of (absolute year, month, day of month).
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.
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.
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.
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
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.
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.
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"
readonly nativeLocal: Date
readonly nativeUtc: Date
readonly era: Era
readonly year: number
readonly yearOfEra: number
readonly weekBasedYear: number
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.
readonly month: Month
readonly weekOfWeekBasedYear: number
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.
readonly dayOfYear: number
readonly dayOfWeekBasedYear: number
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.
readonly dayOfMonth: number
readonly dayOfWeek: DayOfWeek
readonly epochDay: number
readonly quarterOfYear: number
readonly isLeapYear: boolean
readonly lengthOfYear: number
readonly atStartOfDay: LocalDateTime
readonly truncateToYear: LocalDate
readonly truncateToWeekBasedYear: LocalDate
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.
readonly truncateToMonth: LocalDate
readonly truncateToWeek: LocalDate
(other: LocalDate): number
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.
(other: LocalDate): boolean
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.
(other: LocalDate): boolean
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.
(other: LocalDate): boolean
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.
(time: LocalTime): LocalDateTime
(period: Period): LocalDate
(period: Period): LocalDate
(year: number): LocalDate
(month: number | Month): LocalDate
(dayOfMonth: number): LocalDate
(dayOfWeek: number | DayOfWeek): LocalDate
(dayOfYear: number): LocalDate
(): string
(year: number, month: number | Month = JANUARY, dayOfMonth: number = 1): LocalDate
(epochDay: number): LocalDate
(year: number, dayOfYear: number): LocalDate
(year: number, week: number, dayOfWeek: number | DayOfWeek): LocalDate
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.
(year: number, dayOfWeekBasedYear: number): LocalDate
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.
(date: Date): LocalDate
(date: Date): LocalDate
(str: string): LocalDate throws TemporalParsingError
(x: LocalDate, y: LocalDate): number
(x: LocalDate, y: LocalDate): boolean
(x: LocalDate, y: LocalDate): boolean
(x: LocalDate, y: LocalDate): boolean