@@ -5,14 +5,18 @@ import { deprecated } from '../../internal/deprecated';
55
66/**
77 * Converts date passed as a string, number or Date to a Date object.
8- * If nothing or a non parsable value is passed, takes current date .
8+ * If nothing or a non parsable value is passed, then it will take the value from the given fallback .
99 *
10- * @param date Date
10+ * @param date The date to convert.
11+ * @param fallback The fallback date to use if the passed date is not valid.
1112 */
12- function toDate ( date ?: string | Date | number ) : Date {
13+ function toDate (
14+ date ?: string | Date | number ,
15+ fallback : ( ) => Date = ( ) => new Date ( )
16+ ) : Date {
1317 date = new Date ( date ) ;
1418 if ( isNaN ( date . valueOf ( ) ) ) {
15- date = new Date ( ) ;
19+ date = fallback ( ) ;
1620 }
1721
1822 return date ;
@@ -38,7 +42,7 @@ export class DateModule {
3842 *
3943 * @param options The optional options object.
4044 * @param options.years The range of years the date may be in the past. Defaults to `1`.
41- * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
45+ * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
4246 *
4347 * @see faker.date.recent()
4448 *
@@ -59,15 +63,15 @@ export class DateModule {
5963 /**
6064 * The date to use as reference point for the newly generated date.
6165 *
62- * @default new Date ()
66+ * @default faker.defaultRefDate ()
6367 */
6468 refDate ?: string | Date | number ;
6569 } ) : Date ;
6670 /**
6771 * Generates a random date in the past.
6872 *
6973 * @param years The range of years the date may be in the past. Defaults to `1`.
70- * @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
74+ * @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
7175 *
7276 * @see faker.date.recent()
7377 *
@@ -86,7 +90,7 @@ export class DateModule {
8690 *
8791 * @param options The optional options object.
8892 * @param options.years The range of years the date may be in the past. Defaults to `1`.
89- * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
93+ * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
9094 * @param legacyRefDate Deprecated, use `options.refDate` instead.
9195 *
9296 * @see faker.date.recent()
@@ -111,7 +115,7 @@ export class DateModule {
111115 /**
112116 * The date to use as reference point for the newly generated date.
113117 *
114- * @default new Date ()
118+ * @default faker.defaultRefDate ()
115119 */
116120 refDate ?: string | Date | number ;
117121 } ,
@@ -142,7 +146,7 @@ export class DateModule {
142146 throw new FakerError ( 'Years must be greater than 0.' ) ;
143147 }
144148
145- const date = toDate ( refDate ) ;
149+ const date = toDate ( refDate , this . faker . defaultRefDate ) ;
146150 const range = {
147151 min : 1000 ,
148152 max : years * 365 * 24 * 3600 * 1000 ,
@@ -160,7 +164,7 @@ export class DateModule {
160164 *
161165 * @param options The optional options object.
162166 * @param options.years The range of years the date may be in the future. Defaults to `1`.
163- * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
167+ * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
164168 *
165169 * @see faker.date.soon()
166170 *
@@ -181,15 +185,15 @@ export class DateModule {
181185 /**
182186 * The date to use as reference point for the newly generated date.
183187 *
184- * @default new Date ()
188+ * @default faker.defaultRefDate ()
185189 */
186190 refDate ?: string | Date | number ;
187191 } ) : Date ;
188192 /**
189193 * Generates a random date in the future.
190194 *
191195 * @param years The range of years the date may be in the future. Defaults to `1`.
192- * @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
196+ * @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
193197 *
194198 * @see faker.date.soon()
195199 *
@@ -208,7 +212,7 @@ export class DateModule {
208212 *
209213 * @param options The optional options object.
210214 * @param options.years The range of years the date may be in the future. Defaults to `1`.
211- * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
215+ * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
212216 * @param legacyRefDate Deprecated, use `options.refDate` instead.
213217 *
214218 * @see faker.date.soon()
@@ -233,7 +237,7 @@ export class DateModule {
233237 /**
234238 * The date to use as reference point for the newly generated date.
235239 *
236- * @default new Date ()
240+ * @default faker.defaultRefDate ()
237241 */
238242 refDate ?: string | Date | number ;
239243 } ,
@@ -264,7 +268,7 @@ export class DateModule {
264268 throw new FakerError ( 'Years must be greater than 0.' ) ;
265269 }
266270
267- const date = toDate ( refDate ) ;
271+ const date = toDate ( refDate , this . faker . defaultRefDate ) ;
268272 const range = {
269273 min : 1000 ,
270274 max : years * 365 * 24 * 3600 * 1000 ,
@@ -552,7 +556,7 @@ export class DateModule {
552556 *
553557 * @param options The optional options object.
554558 * @param options.days The range of days the date may be in the past. Defaults to `1`.
555- * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
559+ * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
556560 *
557561 * @see faker.date.past()
558562 *
@@ -573,15 +577,15 @@ export class DateModule {
573577 /**
574578 * The date to use as reference point for the newly generated date.
575579 *
576- * @default new Date ()
580+ * @default faker.defaultRefDate ()
577581 */
578582 refDate ?: string | Date | number ;
579583 } ) : Date ;
580584 /**
581585 * Generates a random date in the recent past.
582586 *
583587 * @param days The range of days the date may be in the past. Defaults to `1`.
584- * @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
588+ * @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
585589 *
586590 * @see faker.date.past()
587591 *
@@ -600,7 +604,7 @@ export class DateModule {
600604 *
601605 * @param options The optional options object.
602606 * @param options.days The range of days the date may be in the past. Defaults to `1`.
603- * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
607+ * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
604608 * @param legacyRefDate Deprecated, use `options.refDate` instead.
605609 *
606610 * @see faker.date.past()
@@ -625,7 +629,7 @@ export class DateModule {
625629 /**
626630 * The date to use as reference point for the newly generated date.
627631 *
628- * @default new Date ()
632+ * @default faker.defaultRefDate ()
629633 */
630634 refDate ?: string | Date | number ;
631635 } ,
@@ -651,7 +655,7 @@ export class DateModule {
651655 throw new FakerError ( 'Days must be greater than 0.' ) ;
652656 }
653657
654- const date = toDate ( refDate ) ;
658+ const date = toDate ( refDate , this . faker . defaultRefDate ) ;
655659 const range = {
656660 min : 1000 ,
657661 max : days * 24 * 3600 * 1000 ,
@@ -669,7 +673,7 @@ export class DateModule {
669673 *
670674 * @param options The optional options object.
671675 * @param options.days The range of days the date may be in the future. Defaults to `1`.
672- * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
676+ * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
673677 *
674678 * @see faker.date.future()
675679 *
@@ -690,15 +694,15 @@ export class DateModule {
690694 /**
691695 * The date to use as reference point for the newly generated date.
692696 *
693- * @default new Date ()
697+ * @default faker.defaultRefDate ()
694698 */
695699 refDate ?: string | Date | number ;
696700 } ) : Date ;
697701 /**
698702 * Generates a random date in the near future.
699703 *
700704 * @param days The range of days the date may be in the future. Defaults to `1`.
701- * @param refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
705+ * @param refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
702706 *
703707 * @see faker.date.future()
704708 *
@@ -717,7 +721,7 @@ export class DateModule {
717721 *
718722 * @param options The optional options object.
719723 * @param options.days The range of days the date may be in the future. Defaults to `1`.
720- * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `new Date ()`.
724+ * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate ()`.
721725 * @param legacyRefDate Deprecated, use `options.refDate` instead.
722726 *
723727 * @see faker.date.future()
@@ -742,7 +746,7 @@ export class DateModule {
742746 /**
743747 * The date to use as reference point for the newly generated date.
744748 *
745- * @default new Date ()
749+ * @default faker.defaultRefDate ()
746750 */
747751 refDate ?: string | Date | number ;
748752 } ,
@@ -768,7 +772,7 @@ export class DateModule {
768772 throw new FakerError ( 'Days must be greater than 0.' ) ;
769773 }
770774
771- const date = toDate ( refDate ) ;
775+ const date = toDate ( refDate , this . faker . defaultRefDate ) ;
772776 const range = {
773777 min : 1000 ,
774778 max : days * 24 * 3600 * 1000 ,
@@ -938,7 +942,7 @@ export class DateModule {
938942 /**
939943 * The date to use as reference point for the newly generated date.
940944 *
941- * @default new Date ()
945+ * @default faker.defaultRefDate ()
942946 */
943947 refDate ?: string | Date | number ;
944948 } = { }
0 commit comments