TypeScript Reference ยท Primitive and Special Types
TypeScript Date and RegExp types
Reference for the Date and RegExp types in TypeScript: how to type dates and patterns, what exec and match return, named groups, and common pitfalls.
Date and RegExp are two built-in JavaScript classes, and TypeScript describes each one with an interface of the same name. A variable of type Date holds a point in time; a variable of type RegExp holds a compiled pattern. Both are object types, so they are never assignable from a plain string or number, even though you can create them from one.
The types are useful mostly for what they return. Date methods give back number or string, RegExp.prototype.exec gives back RegExpExecArray | null, and String.prototype.match gives back RegExpMatchArray | null. Because those results can be null, strict mode makes you handle the "no match" case before you read a capture group.
const created: Date = new Date(2026, 0, 15); const pattern: RegExp = /(\d+)-(\d+)/;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
interface Date { /* getTime(): number; toISOString(): string; ... */ } interface DateConstructor { new (): Date; new (value: number | string | Date): Date; new (year: number, monthIndex: number, day?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date; (): string; // Date() without new returns a string now(): number; parse(s: string): number; UTC(year: number, monthIndex?: number, ...): number; } interface RegExp { exec(string: string): RegExpExecArray | null; test(string: string): boolean; readonly source: string; readonly flags: string; lastIndex: number; }
| Name | Description |
|---|---|
new Date(...) |
Creates a Date from nothing (now), a timestamp, a string or date parts. Months are 0-based. |
Date.now() |
Returns the current timestamp as a number. |
exec(text) |
Returns a RegExpExecArray with index, input and optional groups, or null. |
test(text) |
Returns a boolean. |
Returns: getTime(), getFullYear() and friends return number; toISOString() and toString() return string. Setters return the new timestamp as a number.
Edge cases: an invalid date is still a Date; its getTime() is NaN. groups is typed { [key: string]: string } | undefined, and every capture entry such as m[1] is typed string (not string | undefined) unless noUncheckedIndexedAccess is on.
Usage notes
- A
Dateis mutable. Setters such assetDatechange the object in place, so copy before you modify, asaddDaysabove does. If you want to forbid mutation in a type, useReadonly<Date>only as a hint; it does not remove the setters (they are methods), so prefer passing timestamps or ISO strings. new Date("2026-03-14")parses as UTC midnight, whilenew Date("2026-03-14T00:00")parses as local time. Prefer ISO strings with an explicitZor offset to keep results deterministic.- Calling
Date()withoutnewreturns a string, and TypeScript's declaration reflects that. Always usenew Date(). - Subtracting two dates (
b - a) is rejected by the compiler. Useb.getTime() - a.getTime(). - JSON turns dates into strings. After
JSON.parse, a field you typed asDateis really astringuntil you convert it. test()andexec()on a regex with thegoryflag are stateful throughlastIndex. Reusing a global regex literal in a loop can skip matches.- With named groups,
m.groupsis typed loosely; the compiler does not know thatyearexists. Narrow it yourself or validate the shape. String.prototype.matchAllreturns an iterator ofRegExpExecArrayand needs thegflag at runtime.- The
TemporalAPI is the modern replacement forDate, but it is not yet part of the standard lib types TypeScript ships by default; treatDateas the type you will meet in most code.