Skip to content

Getting Started with Date

Advantages

  • Tiny, dependency-free replacement for the most common Date chores
  • Two-way formatDate conversion using the same token string
  • Built-in timezone handling and "active time" (time that keeps ticking)
  • Works in both browsers and Node.js — pure computation, no DOM access required

Import Module

typescript
import { date } from "jsfast";

Get a time string

A lot of projects need to print a time as a string – Date.toString() is rarely what you want and Intl.DateTimeFormat is heavy. date.string takes the same token string as everything else in this module.

Format string

If you don't pass a format, the default is "Y-M-D h:m:s".

The complete list of tokens is in the Format Table below.

typescript
date.string("Y-M-D h:m:s"); // current time
date.string("Y-M-D h:m:s", 1666351246000); // convert a timestamp
date.string(); // same as date.string("Y-M-D h:m:s")

Type Declaration

typescript
function string(format?: string, date?: number): string;

Parse a date string

Some date pickers hand you a string and you need a timestamp (or a Date) back. date.parse is the main entry point.

typescript
date.parse(1666351246000);

Using parse

Format string

If you omit format, it defaults to "Y-M-D h:m:s".

Misunderstanding

The format you pass to parse describes the input string, not the output string. To re-format an existing string see reformat.

parse takes a string, number or Date and returns a parseReturn object. There are a few convenience wrappers for clarity:

typescript
date.parse("2022-10-21 19:20:46", "Y-M-D h:m:s");
date.parse(1666351246000);
date.parseDate("2022-10-21 19:20:46", "Y-M-D h:m:s"); // alias for "input is a string"
date.parseTime(1666351246000, "Y-M-D h:m:s"); // alias for "input is a timestamp"
date.now("Y-M-D h:m:s"); // current time as parseReturn

Missing fields

When the input string only covers part of the format (for example just "2024" against "Y"), the year/month/day fields fall back to the current date and the time fields fall back to 0. The result is built in one step via new Date(y, m, d, h, mi, s, ms), so a value like "31-01" will never roll over into the next month.

Return object

parse and its friends all return a parseReturn:

KeyDescriptionExample
formatformat string actually usedY-M-D h:m:s
datelocal Date object2022-10-21T11:20:46.000Z
stringformatted local string (same as dateString)2022-10-21 19:20:46
dateStringformatted local string2022-10-21 19:20:46
timestampUTC timestamp (milliseconds)1666351246000
utcDateDate shifted by the current timezone2022-10-21T19:20:46.000Z
utcTimestamptimestamp shifted by the current timezone1666380046000
utcDateStringformatted string from utcTimestamp2022-10-21 19:20:46

Type Declaration

typescript
function parse(time: string | number | Date, format?: string): parseReturn;
function parseDate(date: string, format?: string): parseReturn;
function parseTime(time: number, format?: string): parseReturn;
function now(format?: string): parseReturn;

interface parseReturn {
  format: string;
  date: Date;
  string: string;
  dateString: string;
  timestamp: number;
  utcDate: Date;
  utcTimestamp: number;
  utcDateString: string;
}

Reformat a date string

Simplified parse

date.reformat is a thin wrapper around parse: it only returns the formatted string.

typescript
date.reformat("D/M/Y", "21/10/2022", "Y-M-D");
// → "2022-10-21 00:00:00"

date.reformat("<Today is> Y-M-D", "Today is 2022-10-21", "Y/M/D");
// → "2022/10/21"

Type Declaration

typescript
function reformat(format: string, date: string, newFormat?: string): string;

Format Table

Single- and double-character tokens (m/mm, s/ss, h/hh) have the same meaning. Both are padded to two digits on output. Y is padded to four digits and S to three.

TokenDescriptionExample
YYear (4 digits)2025
MMonth, 1–1206
DDay, 1–3119
hHour, 24-hour, 0–2309
hhSame as h (kept for visual consistency)09
HHour, 12-hour, 1–1209
mMinute, 0–5907
mmSame as m07
sSecond, 0–5932
ssSame as s32
SMillisecond (3 digits)045
AAM / PMAM
aam / pmam
<...>Literal segment, kept verbatim, not parsed<Now>

Common pitfalls

  • H parses as 12-hour and requires A or a to disambiguate, otherwise an error is thrown in development mode.
  • Two parse-relevant tokens cannot sit next to each other in the same format string (e.g. "hh" is treated as the same token, but "hm" two distinct tokens). For ambiguous outputs add a separator (e.g. "h:m").

Literal segments with < >

Any text inside < ... > is left as-is and ignored by the parser, so it round-trips perfectly:

typescript
date.string("<Today is> Y-M-D");
// → "Today is 2025-06-19"

date.parseDate("Today is 2025-06-19", "<Today is> Y-M-D").date;
// → Date for 2025-06-19

You can use it to mix in plain text (Chinese, Japanese, emoji, …) without escaping individual characters.