diff options
| author | ST-DDT <[email protected]> | 2023-04-26 06:16:51 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-26 06:16:51 +0200 |
| commit | 3fc7bf1b24ed67696b4824abcd9fa14af43cb66d (patch) | |
| tree | aeead10dc7e2ab4a31efd90fc4a5e342b8543f27 /src/modules/date | |
| parent | 4d0458c96071917c8c3bb85fa61544caf8ff1763 (diff) | |
| download | faker-3fc7bf1b24ed67696b4824abcd9fa14af43cb66d.tar.xz faker-3fc7bf1b24ed67696b4824abcd9fa14af43cb66d.zip | |
feat(date): introduce anytime (#2096)
Diffstat (limited to 'src/modules/date')
| -rw-r--r-- | src/modules/date/index.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index ad8be056..feac3692 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -52,6 +52,41 @@ export class DateModule { } /** + * Generates a random date that can be either in the past or in the future. + * + * @param options The optional options object. + * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. + * + * @see faker.date.between() For dates in a specific range. + * @see faker.date.past() For dates explicitly in the past. + * @see faker.date.future() For dates explicitly in the future. + * + * @example + * faker.date.anytime() // '2022-07-31T01:33:29.567Z' + * + * @since 8.0.0 + */ + anytime( + options: { + /** + * The date to use as reference point for the newly generated date. + * + * @default faker.defaultRefDate() + */ + refDate?: string | Date | number; + } = {} + ): Date { + const { refDate } = options; + + const date = toDate(refDate, this.faker.defaultRefDate); + + return this.between({ + from: new Date(date.getTime() - 1000 * 60 * 60 * 24 * 365), + to: new Date(date.getTime() + 1000 * 60 * 60 * 24 * 365), + }); + } + + /** * Generates a random date in the past. * * @param options The optional options object. |
