@pbkware/dot-net-date-number-formatting - v0.3.2
    Preparing search index...

    Class DotNetDateTimeFormatter

    Formatter for dates and times using .NET-compatible format strings.

    Supports both standard format strings (d, D, f, F, g, G, M, O, s, t, T, u, Y) and custom format strings with specific date/time components (yyyy, MM, dd, HH, mm, ss, etc.).

    const formatter = new DotNetDateTimeFormatter();
    formatter.localeSettings = DotNetLocaleSettings.createInvariant();

    // Standard format strings
    formatter.trySetFormat('d'); // Short date
    console.log(formatter.toString(new Date(2024, 6, 5))); // "7/5/2024"

    formatter.trySetFormat('F'); // Full date/time
    console.log(formatter.toString(new Date(2024, 6, 5, 14, 30, 0)));
    // "Friday, July 5, 2024 2:30:00 PM"

    // Custom format strings
    formatter.trySetFormat('yyyy-MM-dd');
    console.log(formatter.toString(new Date(2024, 6, 5))); // "2024-07-05"

    formatter.trySetFormat("dddd, MMMM d 'at' h:mm tt");
    console.log(formatter.toString(new Date(2024, 6, 5, 14, 30, 0)));
    // "Friday, July 5 at 2:30 PM"
    Index
    localeSettings: DotNetLocaleSettings = DotNetLocaleSettings.current

    The locale settings that determine date/time separators and formatting conventions.

    parseErrorText: string = ""

    Contains the error message from the last failed operation. Check this property if DotNetDateTimeFormatter.trySetFormat returns an error.

    styles: number = DotNetDateTimeStyles.none

    The (DotNetDateTimeStyles:type) flags that control date/time parsing behavior.

    Use predefined combinations from (DotNetDateTimeStyles:var) or combine (DotNetDateTimeStyleFlags:var) using bitwise OR.

    // Use predefined combination
    formatter.styles = DotNetDateTimeStyles.allowWhiteSpaces;

    // Combine individual flags
    formatter.styles =
    DotNetDateTimeStyleFlags.allowLeadingWhite |
    DotNetDateTimeStyleFlags.allowTrailingWhite;
    unsupportedStyles: (16 | 32 | 64 | 128)[] = ...

    Array of date/time style flags that are not currently supported by this implementation. Operations using these styles will fail.

    • Formats a Date using the current format string.

      Parameters

      • value: Date

        The Date to format.

      Returns string

      The formatted date/time string.

      const date = new Date(2024, 6, 5, 14, 30, 45);

      formatter.trySetFormat('d');
      console.log(formatter.toString(date)); // "7/5/2024"

      formatter.trySetFormat('yyyy-MM-dd HH:mm:ss');
      console.log(formatter.toString(date)); // "2024-07-05 14:30:45"
    • Sets the format string to use for formatting dates and times.

      The format string is tokenized to confirm validity and to optimize formatting/parsing operations.

      Parameters

      • value: string

        A standard format string (single character like 'd', 'F', 'o') or custom format string (e.g., "yyyy-MM-dd HH:mm:ss").

      Returns Result<void>

      A Result indicating success or containing an error message if the format string is invalid.

      // Standard format
      formatter.trySetFormat('d'); // Short date

      // Custom format
      formatter.trySetFormat('yyyy-MM-dd HH:mm:ss');

      // Check for errors
      const result = formatter.trySetFormat('');
      if (result.isErr()) {
      console.error(result.error); // "Format text cannot be empty"
      }