aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2023-04-26 06:16:51 +0200
committerGitHub <[email protected]>2023-04-26 06:16:51 +0200
commit3fc7bf1b24ed67696b4824abcd9fa14af43cb66d (patch)
treeaeead10dc7e2ab4a31efd90fc4a5e342b8543f27 /src
parent4d0458c96071917c8c3bb85fa61544caf8ff1763 (diff)
downloadfaker-3fc7bf1b24ed67696b4824abcd9fa14af43cb66d.tar.xz
faker-3fc7bf1b24ed67696b4824abcd9fa14af43cb66d.zip
feat(date): introduce anytime (#2096)
Diffstat (limited to 'src')
-rw-r--r--src/modules/datatype/index.ts5
-rw-r--r--src/modules/date/index.ts35
2 files changed, 38 insertions, 2 deletions
diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts
index e776784c..0c5fddc8 100644
--- a/src/modules/datatype/index.ts
+++ b/src/modules/datatype/index.ts
@@ -166,6 +166,7 @@ export class DatatypeModule {
* When not provided or larger than `8640000000000000`, `2100-01-01` is considered
* as maximum generated date. Defaults to `4102444800000`.
*
+ * @see faker.date.anytime()
* @see faker.date.between()
*
* @example
@@ -175,7 +176,7 @@ export class DatatypeModule {
*
* @since 5.5.0
*
- * @deprecated Use `faker.date.between({ from: min, to: max })` instead.
+ * @deprecated Use `faker.date.between({ from: min, to: max })` or `faker.date.anytime()` instead.
*/
datetime(
options:
@@ -201,7 +202,7 @@ export class DatatypeModule {
): Date {
deprecated({
deprecated: 'faker.datatype.datetime({ min, max })',
- proposed: 'faker.date.between({ from, to })',
+ proposed: 'faker.date.between({ from, to }) or faker.date.anytime()',
since: '8.0',
until: '9.0',
});
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.