Documentation v2.0.3

Date and Number Extensions

Date-part getters, the $format formatter, and relative time on Date.prototype, plus timestamp and random-key helpers on Number.prototype.

Date

Source: src/prototype/Date.ts

Method / Property Description
date.$y / $yy / $yyyy Year (raw, 2-digit, 4-digit)
date.$M / $MM Month, 1-indexed (raw, zero-padded)
date.$D / $DD Day of month (raw, zero-padded)
date.$d / $dd / $ddd / $dddd Day of week: index, then localized name at increasing length (English abbreviations or 星期X in zh locales, detected via navigator.language)
date.$H / $HH 24-hour hour (raw, zero-padded)
date.$h / $hh 12-hour hour (raw, zero-padded)
date.$a / $A am/pm (lower/upper case)
date.$m / $mm Minutes (raw, zero-padded)
date.$s / $ss Seconds (raw, zero-padded)
date.$S / $SS / $SSS Milliseconds, sliced to 1/2/3 digits
date.$timestamp Unix timestamp in seconds
date.$gone Humanized relative time ("3 days" / "3天", "Coming in..." / "還有...", "recent" / "剛剛" for < 1 minute), localized by navigator.language
date.$format(format = "yyyy/MM/DD (ddd) HH:mm:ss") Token-based formatter; supports Y/M/D/H/h/m/s/S/A/a/d tokens at the repeat-counts shown above
date.$date(body?) Given { start } / { end } / { pre: { start } } / { pre: { end } }, returns the first/last day of the current or previous month; otherwise new Date()

Number

Source: src/prototype/Number.ts

Method / Property Description
n.$uniKey Random string of length n, drawn from a 52-character alphabet, deduplicated against previously generated keys for the process lifetime
n.$ASCII String.fromCharCode(n)
n.$date Treats n as a Unix timestamp (seconds) and returns the corresponding Date
n.$y / $M / $D / $d / $H / $h / $a / $A / $m / $s / $S (and their padded/repeated variants) / $gone Forwarded to n.$date's equivalent Date getter
n.$format(format) Forwarded to n.$date.$format(format)

Example

Date parts come from local time; $gone and day names follow navigator.language.

<script>
    const date = new Date(2024, 0, 15, 9, 5, 7);
    console.log(date.$yyyy, date.$MM, date.$DD, date.$HH, date.$hh, date.$A);
    console.log(date.$format("yyyy-MM-DD HH:mm:ss"));
    console.log(date.$timestamp.$format("yyyy/MM/DD"));
    console.log(new Date(Date.now() - 3 * 86400000).$gone);
    console.log((8).$uniKey.length, (65).$ASCII);
</script>
Live preview · @pardnchiu/renderjs@2.0.3
中文