js-joda

repository·main·Indexed 23 days ago

https://github.com/js-joda/js-joda

An immutable, domain-driven date and time library for JavaScript. A port of the ThreeTen backport, it provides a robust alternative to the native JavaScript Date object and Moment.js. The project consists of several packages, including @js-joda/core for core functionality, @js-joda/timezone for IANA time zone support, and @js-joda/locale for localized date-time formatting and parsing using CLDR data.

Tokens
26.3K
Snippets
66
Records
90
Agent score
80%

What's inside js-joda

  1. Use @js-joda/locale_en-gb for British English locale functionality

    main
    The @js-joda/locale_en-gb package provides additional date-time classes and functionality specifically for the en-GB (British English) locale. It complements @js-joda/core by implementing locale-specific features that are not present in the core library, most notably pattern elements used for printing and parsing dates according to British English conventions.
  2. Use @js-joda/locale_en-us for US English locale functionality

    main
    The @js-joda/locale_en-us package provides additional date-time classes and functionality specifically for the en-US locale. It complements @js-joda/core by implementing locale-specific features that are not present in the core library, most notably pattern elements used for printing and parsing dates according to US English conventions.
  3. Use @js-joda/locale_en for English locale-specific date-time functionality

    main
    The @js-joda/locale_en package provides additional date-time classes and functionality specifically for the en.* locales. It complements @js-joda/core by implementing locale-specific features that are not present in the core library, most notably providing pattern elements used to print and parse dates according to English locale conventions.
  4. Use @js-joda/locale_nn-no for Nynorsk (Norway) locale support

    main
    The @js-joda/locale_nn-no package provides additional date-time functionality specifically for the nn and nn-NO locales. It extends the core js-joda functionality by implementing locale-specific pattern elements used for printing and parsing dates that are not available in @js-joda/core.
  5. Use locale-specific date-time classes for {{locale}}

    main

    The @js-joda/locale packages provide additional date-time classes and functionality that complement @js-joda/core. These packages implement locale-specific functionality not found in the core library, specifically providing pattern elements required to print and parse dates according to the rules of a specific locale (e.g., {{locale}}).

    To use these features, you must install the specific locale package corresponding to your needs (e.g., @js-joda/locale_en). For general information on how these packages integrate with the ecosystem, refer to the main @js-joda/locale package.

  6. Overview of js-joda domain models

    main

    js-joda uses a domain-driven design with specific classes for different temporal use cases. This is a standalone implementation (not a wrapper around the native JS Date object) based on the ISO-8601 calendar system.

    Dates and Times

    • LocalDate: A date without time or timezone (e.g., 2007-12-24).
    • LocalTime: A time without a timezone (e.g., 11:55:00).
    • LocalDateTime: A combination of LocalDate and LocalTime.
    • ZonedDateTime: A date-time with a timezone (e.g., 2007-12-24T16:15:30+01:00 UTC+01:00).
    • Instant: A point on the timeline measured from the epoch 1970-01-01T00:00:00Z.

    Durations and Periods

    • Duration: A time-based amount of time (e.g., 34.5 seconds).
    • Period: A date-based amount of time (e.g., 2 years, 3 months and 4 days).

    Additional Value Types

    • Year, YearMonth, Month, MonthDay, and DayOfWeek.
  7. What is a LocalDate?

    main
    A LocalDate represents a date with no time and no time zone in the ISO-8601 calendar system (e.g., 2007-12-24). It is an immutable object, meaning any operations that modify the date will return a new LocalDate instance rather than modifying the existing one.
  8. Calculate values across daylight savings transitions

    main

    When performing arithmetic on a ZonedDateTime, the behavior depends on whether you use date units or time units:

    • Date units (e.g., plusWeeks, plusDays): Calculations are performed on the local timeline. The local time remains the same even if a daylight savings transition occurs.
    • Time units (e.g., plusHours, plusMinutes): Calculations are performed on the instant timeline. The absolute point in time is advanced, which may result in a different local time if a daylight savings transition occurs.
    // assume the system default time zone is CET; we define a time as 2016-03-18 at 17:00 local time
    var zdt = ZonedDateTime.parse("2016-03-18T17:00+01:00[Europe/Berlin]");
    
    // adding a date unit of 2 weeks, crossing a daylight saving transition
    zdt.plusWeeks(2); // 2016-04-01T17:00+02:00[Europe/Berlin] (still 17:00)
    
    // adding a time unit of 2 weeks (2 * 7 * 24)
    zdt.plusHours(2 * 7 * 24); // 2016-04-01T18:00+02:00[Europe/Berlin] (now 18:00)
  9. Use Period for date-based amounts of time

    main

    A Period represents a date-based amount of time in the ISO-8601 calendar system (e.g., '2 years, 3 months and 4 days'). Unlike Duration, which is time-based (seconds/nanoseconds), Period is used for calendar-based calculations like years, months, and days.

    Common operations include:

    • Parsing: Create a Period from an ISO-8601 string using Period.parse().
    • Creation: Use factory methods like Period.of(years, months, days) or specific methods like Period.ofYears(n).
    • Manipulation: Use plusDays(), plusMonths(), minusDays(), etc., to modify a period. Note that these operations are immutable and return a new Period instance.
    • Normalization: Use .normalized() to convert a period like '1 year and 37 months' into its standard form (e.g., '2 years and 1 month').
    • Date Arithmetic: Add a Period to a LocalDate or LocalDateTime using the .plus() method.
    • Difference Calculation: Calculate the period between two LocalDate instances using Period.between(start, end).
    // parse and format ISO 8601 period strings
    Period.parse("P1Y10M").toString(); // 'P1Y10M'
    
    // obtain a Period of 10 years, 5 month and 30 days
    Period.of(10, 5, 30).toString(); // 'P10Y5M30D'
    
    // 10 years
    Period.ofYears(10).toString(); // 'P10Y'
    
    // add 45 days to a Period
    Period.ofYears(10)
      .plusDays(45)
      .toString(); // 'P10Y45D'
    
    // normalize a Period of years and month
    Period.of(1, 37, 0)
      .normalized()
      .toString(); // 'P4Y1M'
    
    // add/subtract from a Period
    Period.ofYears(10)
      .plusMonths(10)
      .minusDays(42)
      .toString(); // 'P10Y10M-42D'
    
    // add a Period to LocalDate
    var p = Period.ofMonths(1);
    LocalDate.parse("2012-12-12").plus(p); // '2013-01-12';
    LocalDate.parse("2012-01-31").plus(p); // '2012-02-29';
    LocalDateTime.parse("2012-05-31T12:00").plus(p); // '2012-06-30T12:00';
    
    // calculate the Period between two Dates
    Period.between(LocalDate.parse("2012-06-30"), LocalDate.parse("2012-08-31")); // 'P2M1D'
  10. Understand js-joda immutability and error handling

    main

    The js-joda API is designed around two core principles:

    1. Immutability: Existing instances are never modified. All manipulation methods (such as .plus(), .at(), .with(), etc.) return a new instance representing the result of the operation.
    2. Validity: Instances are always valid. If you attempt to create an invalid date or time (e.g., February 30th), the library will throw an exception rather than returning null or undefined.