From 3fc7bf1b24ed67696b4824abcd9fa14af43cb66d Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Wed, 26 Apr 2023 06:16:51 +0200 Subject: feat(date): introduce anytime (#2096) --- src/modules/date/index.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/modules/date') 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 @@ -51,6 +51,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. * -- cgit v1.2.3