Utility class for handling date and timestamp formatting and calculations. All timestamps are handled in UTC to ensure consistency across different timezones.

Constructors

Methods

  • Formats a timestamp into a human-readable date string. Supports multiple output formats and handles both Unix timestamps and ISO date strings.

    Parameters

    • timestamp: string | number

      Unix timestamp (seconds) or ISO date string

    • Optionalstyle: "full" | "date" | "unix" = "full"

      Format style

    Returns string

    Formatted date string

    If the timestamp is invalid or cannot be parsed

    // Returns "2024-02-07 12:00:00"
    DateFormatter.formatDate(1707307200, "full")
    // Returns "2024-02-07"
    DateFormatter.formatDate(1707307200, "date")
    // Returns "1707307200"
    DateFormatter.formatDate(1707307200, "unix")
  • Formats a timestamp into a human-readable date string. Always returns the full format (YYYY-MM-DD HH:mm:ss) in UTC.

    Parameters

    • timestamp: string | number

      Unix timestamp (seconds) or ISO date string

    Returns string

    Formatted date string in "YYYY-MM-DD HH:mm:ss" format (UTC)

    // Returns "2024-02-07 12:00:00"
    DateFormatter.formatTimestamp(1707307200)
    // Returns "2024-02-07 12:00:00"
    DateFormatter.formatTimestamp("2024-02-07T12:00:00Z")
  • Gets the current Unix timestamp in seconds.

    Returns number

    Current Unix timestamp

    // Returns current timestamp (e.g., 1707307200)
    DateFormatter.getCurrentTimestamp()
  • Gets the Unix timestamp from 24 hours ago. Useful for querying data from the last day.

    Returns number

    Unix timestamp from 24 hours ago

    // Returns timestamp from 24 hours ago
    DateFormatter.get24HoursAgo()
  • Gets the Unix timestamp from a specified number of days ago. Supports fractional days for more precise time ranges.

    Parameters

    • days: number

      Number of days to go back (can be fractional)

    Returns number

    Unix timestamp from specified days ago

    // Returns timestamp from 7 days ago
    DateFormatter.getTimeAgo(7)
    // Returns timestamp from 12 hours ago
    DateFormatter.getTimeAgo(0.5)