Date
Module to generate dates.
Overview
To quickly generate a date in the past, use recent()
(last day) or past()
(last year). To quickly generate a date in the future, use soon()
(next day) or future()
(next year). For a realistic birthdate for an adult, use birthdate()
.
For more control, any of these methods can be customized with further options, or use between()
to generate a single date between two dates, or betweens()
for multiple dates.
If you need to generate a date range (start-end), you can do so using either of these two methods:
const start = faker.date.soon(); const end = faker.date.soon({ refDate: start });
const [start, end] = faker.date.betweens({ from, to, count: 2 });
// does not work with tsconfig'snoUncheckedIndexedAccess: true
Dates can be specified as Javascript Date objects, strings or UNIX timestamps. For example to generate a date between 1st January 2000 and now, use:
faker.date.between({ from: '2000-01-01', to: Date.now() });
You can generate random localized month and weekday names using month()
and weekday()
.
These methods have additional concerns about reproducibility, see Reproducible Results.
anytime
Generates a random date that can be either in the past or in the future.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.refDate? | string | number | Date | faker.defaultRefDate() | The date to use as reference point for the newly generated date. |
Returns: Date
function anytime(
options: {
refDate?: string | Date | number;
} = {}
): Date;
Examples
faker.date.anytime() // '2025-02-05T14:12:45.320Z'
See Also
Source
between
Generates a random date between the given boundaries.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | The options object. | |
options.from | string | number | Date | The early date boundary. | |
options.to | string | number | Date | The late date boundary. |
Returns: Date
Throws: If from
or to
are not provided.
If from
is after to
.
function between(options: {
from: string | Date | number;
to: string | Date | number;
}): Date;
Examples
faker.date.between({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z' }) // '2025-06-27T19:34:39.059Z'
Source
betweens
Generates random dates between the given boundaries. The dates will be returned in an array sorted in chronological order.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | The options object. | |
options.count? | number | { min: number; max: number; } | 3 | The number of dates to generate. |
options.from | string | number | Date | The early date boundary. | |
options.to | string | number | Date | The late date boundary. |
Returns: Date[]
Throws: If from
or to
are not provided.
If from
is after to
.
function betweens(options: {
from: string | Date | number;
to: string | Date | number;
count?:
| number
| {
min: number;
max: number;
};
}): Date[];
Examples
faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z' }) // [ '2025-06-27T19:34:39.059Z', '2026-01-10T21:28:14.545Z', '2027-02-25T14:04:55.663Z' ]
faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: 2 }) // [ '2024-03-27T14:39:48.843Z', '2025-06-13T10:59:54.311Z' ]
faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: { min: 2, max: 5 }}) // [ '2023-11-01T17:05:05.418Z', '2024-05-17T12:08:45.549Z', '2028-12-01T15:31:21.089Z', '2029-08-21T06:14:29.540Z' ]
Source
birthdate
Returns a random birthdate. By default, the birthdate is generated for an adult between 18 and 80 years old.
But you can customize the 'age'
range or the 'year'
range to generate a more specific birthdate.
Available since v7.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options? | { ... } | { ... } | {} | The options to use to generate the birthdate. |
options.refDate? | string | number | Date | faker.defaultRefDate() | The date to use as reference point for the newly generated date. |
options.max | number | The maximum age/year to generate a birthdate for/in. | |
options.min | number | The minimum age/year to generate a birthdate for/in. | |
options.mode | 'age' | 'year' | Either | |
options.refDate? | string | number | Date | faker.defaultRefDate() | The date to use as reference point for the newly generated date.
Only used when |
Returns: Date
function birthdate(
options?:
| {
refDate?: string | Date | number;
}
| {
mode: 'age' | 'year';
min: number;
max: number;
refDate?: string | Date | number;
}
): Date;
Examples
faker.date.birthdate() // '1978-07-30T03:46:09.872Z'
faker.date.birthdate({ mode: 'age', min: 18, max: 65 }) // '1993-04-30T22:38:05.154Z'
faker.date.birthdate({ mode: 'year', min: 1900, max: 2000 }) // '1960-11-17T03:11:01.233Z'
Source
future
Generates a random date in the future.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.refDate? | string | number | Date | faker.defaultRefDate() | The date to use as reference point for the newly generated date. |
options.years? | number | 1 | The range of years the date may be in the future. |
Returns: Date
function future(
options: {
years?: number;
refDate?: string | Date | number;
} = {}
): Date;
Examples
faker.date.future() // '2025-07-20T06:36:23.111Z'
faker.date.future({ years: 10 }) // '2032-02-24T09:35:18.864Z'
faker.date.future({ years: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2026-01-09T02:04:18.675Z'
Source
month
Returns a random name of a month.
Available since v3.0.1
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options to use. |
options.abbreviated? | boolean | false | Whether to return an abbreviation. |
options.context? | boolean | false | Whether to return the name of a month in the context of a date. In the default |
Returns: string
function month(
options: {
abbreviated?: boolean;
context?: boolean;
} = {}
): string;
Examples
faker.date.month() // 'June'
faker.date.month({ abbreviated: true }) // 'May'
faker.date.month({ context: true }) // 'March'
faker.date.month({ abbreviated: true, context: true }) // 'Jun'
Source
past
Generates a random date in the past.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.refDate? | string | number | Date | faker.defaultRefDate() | The date to use as reference point for the newly generated date. |
options.years? | number | 1 | The range of years the date may be in the past. |
Returns: Date
function past(
options: {
years?: number;
refDate?: string | Date | number;
} = {}
): Date;
Examples
faker.date.past() // '2024-07-20T06:36:22.111Z'
faker.date.past({ years: 10 }) // '2022-02-26T09:35:17.864Z'
faker.date.past({ years: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2016-01-12T02:04:17.675Z'
Source
recent
Generates a random date in the recent past.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.days? | number | 1 | The range of days the date may be in the past. |
options.refDate? | string | number | Date | faker.defaultRefDate() | The date to use as reference point for the newly generated date. |
Returns: Date
function recent(
options: {
days?: number;
refDate?: string | Date | number;
} = {}
): Date;
Examples
faker.date.recent() // '2024-12-31T12:10:16.938Z'
faker.date.recent({ days: 10 }) // '2024-12-29T02:38:42.898Z'
faker.date.recent({ days: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2019-12-28T00:39:46.954Z'
Source
soon
Generates a random date in the near future.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options object. |
options.days? | number | 1 | The range of days the date may be in the future. |
options.refDate? | string | number | Date | faker.defaultRefDate() | The date to use as reference point for the newly generated date. |
Returns: Date
function soon(
options: {
days?: number;
refDate?: string | Date | number;
} = {}
): Date;
Examples
faker.date.soon() // '2025-01-01T12:10:17.938Z'
faker.date.soon({ days: 10 }) // '2025-01-08T02:38:43.898Z'
faker.date.soon({ days: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2020-01-07T00:39:47.954Z'
Source
timeZone
Returns a random IANA time zone name.
The returned time zone is not tied to the current locale.
Available since v9.0.0
Returns: string
function timeZone(): string;
Examples
faker.location.timeZone() // 'Asia/Dili'
See Also
Source
weekday
Returns a random day of the week.
Available since v3.0.1
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | The optional options to use. |
options.abbreviated? | boolean | false | Whether to return an abbreviation. |
options.context? | boolean | false | Whether to return the day of the week in the context of a date. In the default |
Returns: string
function weekday(
options: {
abbreviated?: boolean;
context?: boolean;
} = {}
): string;
Examples
faker.date.weekday() // 'Sunday'
faker.date.weekday({ abbreviated: true }) // 'Tue'
faker.date.weekday({ context: true }) // 'Thursday'
faker.date.weekday({ abbreviated: true, context: true }) // 'Sun'