@ma.vu/appdate
    Preparing search index...

    Class AppDate

    AppDate: A timezone-aware date and time abstraction.

    IMPORTANT: Always use this wrapped abstraction instead of moment or new Date directly. You should not polute the application with randomly used date methods.

    This class wraps the dayjs library to provide a consistent interface for working with dates and times in a specific timezone. It offers methods for date manipulation, comparison, and formatting, while maintaining timezone context.

    Key features:

    • Timezone awareness: All operations respect the specified timezone.
    • Immutability: Operations return new instances, preserving the original.
    • Consistent API: Provides a uniform interface for date operations.
    • Error handling: Gracefully handles invalid dates and parsing errors.

    Use AppDate to ensure consistent date handling across your application, especially when dealing with different timezones or complex date logic.

    const today = AppDate.now();
    const futureDate = today.add(5, 'days');
    console.log(futureDate.toLocalizedDateString());
    Index

    Properties

    dayjsDate: Dayjs
    timezone: string

    Methods

    • Parameters

      • value: number
      • Optionalunit: ManipulateType

      Returns AppDate

    • Get the formatted date according to the string of tokens passed in.

      To escape characters, wrap them in square brackets (e.g. [MM]).

      Parameters

      • template: FormatTemplate = "YYYY-MM-DDTHH:mm:ssZ[Z]"

      Returns string

      format documentation

      TODO: Support date-fns/Unicode format tokens (e.g. EEEE for day of week) or warn when unrecognized tokens are used instead of printing them literally. Currently uses dayjs tokens: dddd = day of week, not EEEE.

    • Parameters

      Returns string

      The short formatted date string.

      const date = AppDate.now();
      console.log(date.formatShort({ includeDayOfWeek: true })); // "Su, 04.01."
      console.log(date.formatShort({ includeDayOfWeek: false })); // "04.01."
      
      
    • Parameters

      • other: AppDate
      • Optionalunit: OpUnitType

      Returns boolean

    • Parameters

      • other: AppDate
      • Optionalunit: OpUnitType

      Returns boolean

    • Parameters

      • from: AppDate = ...
      • to: AppDate = ...
      • Optionalunit: OpUnitType
      • Optionalinclusivity: "()" | "(]" | "[)" | "[]"

      Returns boolean

    • Returns boolean

    • Parameters

      • other: AppDate
      • Optionalunit: OpUnitType

      Returns boolean

    • returns true if date is current day

      Returns boolean

    • Returns boolean

    • Parameters

      • value: number
      • Optionalunit: ManipulateType

      Returns AppDate

    • Converts the current date to a string: YYYY-MM-DD

      Returns `${string}-${string}-${string}`

      A string representing the current date in "YYYY-MM-DD" format.

      const date = AppDate.now();
      console.log(date.toDateString()); // "2026-01-04"
      
      
    • Converts the current date to a epoch milliseconds (timestamp in milliseconds).

      Returns number

      The epoch milliseconds.

      const date = AppDate.now();
      console.log(date.toEpochMillis()); // 1714732800000
      
      
    • Converts the current date to a epoch seconds (unix timestamp).

      Returns number

      The epoch seconds.

      const date = AppDate.now();
      console.log(date.toEpochSeconds()); // 1714732800
      
      
    • Locale friendly format:

      de, ch = 20.10.1985
      us = 10/20/1985
      {includeDayOfWeek: true}
      de, ch = Son, 20.10.1985
      us = Sun, 10/20/1985

      Parameters

      Returns string

    • Converts Date in following string format: HH:mm (20:10)

      Returns string

    • Returns a localized string representing the relative time from now. Automatically handles past and future dates.

      Parameters

      • Optionaloptions: RelativeTimeOptions

        Optional settings for capping and fallback behavior

        • Optionalcap?: number

          Cap the relative time at this many days. Shows "X+" format after this threshold.

        • Optionalfallback?: (date: AppDate) => string

          Custom formatter function called when fallbackAfterDays is exceeded. Defaults to toLocalizedDateString().

        • OptionalfallbackAfterDays?: number

          After this many days, use the fallback formatter instead of relative time.

      Returns string

      A localized string like "2 days ago" or "in 3 hours"

      await setAppDateLanguage('sr');
      AppDate.now().subtract(2, 'day').toRelative(); // "pre 2 dana"
      AppDate.now().add(3, 'hour').toRelative(); // "za 3 sata"

      // With cap at 9 days
      AppDate.now().subtract(15, 'day').toRelative({ cap: 9 }); // "pre 9+ dana"

      // With fallback to date after 14 days
      AppDate.now().subtract(20, 'day').toRelative({
      cap: 9,
      fallbackAfterDays: 14,
      fallback: (d) => d.toLocalizedDateString()
      }); // "20.12.2025"
    • Returns `${string}-${string}-${string}`

    • Returns time in UTC format: HH:mm:ssZ

      Returns string

    • Creates a AppDate instance from a date string.

      Parameters

      • date: string

        A string representing a date in "YYYY-MM-DD" format.

      Returns AppDate

      A new AppDate instance set to the given date.

      If the date string is invalid or cannot be parsed, it returns an invalid AppDate instance. The time part of the created AppDate will be set to midnight in the local timezone.

      const date = AppDate.fromDateString("2023-05-21");
      
    • Creates a AppDate instance from a epoch milliseconds.

      Parameters

      • ms: number

        A number representing a epoch milliseconds.

      Returns AppDate

      A new AppDate instance set to the given epoch milliseconds.

      const date = AppDate.fromEpochMillis(1714732800000);
      console.log(date.toLocalizedDateString()); // "04.01.2026"

      }

    • Creates a AppDate instance from a epoch seconds.

      Parameters

      • seconds: number

        A number representing a epoch seconds.

      Returns AppDate

      A new AppDate instance set to the given epoch seconds.

      const date = AppDate.fromEpochSeconds(1714732800);
      console.log(date.toLocalizedDateString()); // "04.01.2026"
    • Creates a AppDate instance from a local time string.

      Parameters

      • time: string

        A string representing a local time in any valid (24h) time format.

      Returns AppDate

      A new AppDate instance set to the given time on the todays date.

      If the time string is invalid, it returns an invalid AppDate instance. The date part defaults to the current date in the local timezone.

      const date = AppDate.fromLocalTime("14:30"); Today's date, 14:30 or (02:30 PM)
      
    • Parameters

      • Optionaldate: string

        A string representing a UTC date in "YYYY-MM-DD" format.

      Returns AppDate

      A new AppDate instance set to the given UTC date.

      const date = AppDate.fromUtcString("2026-01-04");
      console.log(date.toLocalizedDateString()); // "04.01.2026"
      
      
    • Creates a AppDate instance from a UTC time string.

      Parameters

      • time: string

        A string representing a UTC time in "HH:mm:ssZ" format.

      Returns AppDate

      A new AppDate instance set to the given UTC time.

      const time = AppDate.fromUtcTime("14:30:00+00:00");
      console.log(time.toLocalizedDateString()); // "04.01.2026"
      
      
    • Creates an invalid AppDate instance. Usiful for: Error handeling, default values, avoids null object pattern and plays nicely with validation

      Returns AppDate

    • Returns the maximum supported date (2200-12-31).

      Returns AppDate

      A new AppDate instance set to the maximum supported date.

      const maxDate = AppDate.maxDate();
      console.log(maxDate.toLocalizedDateString()); // "31.12.2200"
      
      
    • Returns AppDate

      A new AppDate instance set to the minimum supported date (1900-01-01).

      const minDate = AppDate.minDate();
      console.log(minDate.toLocalizedDateString()); // "01.01.1900"