Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
const created: Date = new Date(2026, 0, 15);
const pattern: RegExp = /(\d+)-(\d+)/;

Try it

TypeScript Typing dates

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.

TypeScript Typing regular expressions

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

TypeScript
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 Date is mutable. Setters such as setDate change the object in place, so copy before you modify, as addDays above does. If you want to forbid mutation in a type, use Readonly<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, while new Date("2026-03-14T00:00") parses as local time. Prefer ISO strings with an explicit Z or offset to keep results deterministic.
  • Calling Date() without new returns a string, and TypeScript's declaration reflects that. Always use new Date().
  • Subtracting two dates (b - a) is rejected by the compiler. Use b.getTime() - a.getTime().
  • JSON turns dates into strings. After JSON.parse, a field you typed as Date is really a string until you convert it.
  • test() and exec() on a regex with the g or y flag are stateful through lastIndex. Reusing a global regex literal in a loop can skip matches.
  • With named groups, m.groups is typed loosely; the compiler does not know that year exists. Narrow it yourself or validate the shape.
  • String.prototype.matchAll returns an iterator of RegExpExecArray and needs the g flag at runtime.
  • The Temporal API is the modern replacement for Date, but it is not yet part of the standard lib types TypeScript ships by default; treat Date as the type you will meet in most code.