formatDate / timestampToTime
Format a date with a token pattern.
API
formatDate(value?, pattern?)
| Parameter | Description | Type | Default |
|---|---|---|---|
value |
Timestamp, date string or Date; omit for now |
number | string | Date |
now |
pattern |
Token pattern | string |
'YYYY-MM-DD HH:mm:ss' |
| Token | Meaning | Token | Meaning |
|---|---|---|---|
YYYY/YY |
Year | mm/m |
Minute |
MM/M |
Month (1–12) | ss/s |
Second |
DD/D |
Day | SSS |
Milliseconds |
HH/H |
Hour (0–23) | A/a |
AM/PM · am/pm |
hh/h |
Hour (1–12) | [...] |
Literal text |
Returns 'Invalid Date' when the input cannot be parsed.
timestampToTime(timestamp?)
Deprecated. Returns a Date with a format method attached to the instance.
Example
import { formatDate } from 'ranuts';
formatDate(); // '2026-07-25 14:30:00'
formatDate(1753425000000, 'YYYY/MM/DD'); // '2026/07/25'
formatDate(new Date(), 'YYYY[年]MM[月]DD[日] hh:mm a');
formatDate('not a date'); // 'Invalid Date'Notes
- Case is significant.
MMis the month,mmthe minute;HHis 24-hour,hh12-hour. - The pattern is substituted in one pass, so a value just written can never be matched again by a later token.
- Wrap literals in
[]to keep letters out of the substitution.