1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
import type { Faker } from '../..';
import type { DateEntryDefinition } from '../../definitions';
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
/**
* Converts date passed as a string, number or Date to a Date object.
* If nothing or a non parsable value is passed, takes current date.
*
* @param date Date
*/
function toDate(date?: string | Date | number): Date {
date = new Date(date);
if (isNaN(date.valueOf())) {
date = new Date();
}
return date;
}
/**
* Module to generate dates.
*/
export class DateModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(DateModule.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Generates a random date in the past.
*
* @param options The optional options object.
* @param options.years The range of years the date may be in the past. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.recent()
*
* @example
* faker.date.past() // '2021-12-03T05:40:44.408Z'
* faker.date.past({ years: 10 }) // '2017-10-25T21:34:19.488Z'
* faker.date.past({ years: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2017-08-18T02:59:12.350Z'
*
* @since 8.0.0
*/
past(options?: { years?: number; refDate?: string | Date | number }): Date;
/**
* Generates a random date in the past.
*
* @param years The range of years the date may be in the past. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.recent()
*
* @example
* faker.date.past() // '2021-12-03T05:40:44.408Z'
* faker.date.past(10) // '2017-10-25T21:34:19.488Z'
* faker.date.past(10, '2020-01-01T00:00:00.000Z') // '2017-08-18T02:59:12.350Z'
*
* @since 2.0.1
*
* @deprecated Use `faker.date.past({ years, refDate })` instead.
*/
past(years?: number, refDate?: string | Date | number): Date;
/**
* Generates a random date in the past.
*
* @param options The optional options object.
* @param options.years The range of years the date may be in the past. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to now.
* @param legacyRefDate Deprecated, use `options.refDate` instead.
*
* @see faker.date.recent()
*
* @example
* faker.date.past() // '2021-12-03T05:40:44.408Z'
* faker.date.past({ years: 10 }) // '2017-10-25T21:34:19.488Z'
* faker.date.past({ years: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2017-08-18T02:59:12.350Z'
*
* @since 8.0.0
*/
past(
options?:
| number
| {
years?: number;
refDate?: string | Date | number;
},
legacyRefDate?: string | Date | number
): Date;
past(
options:
| number
| {
years?: number;
refDate?: string | Date | number;
} = {},
legacyRefDate?: string | Date | number
): Date {
if (typeof options === 'number') {
deprecated({
deprecated: 'faker.date.past(years, refDate)',
proposed: 'faker.date.past({ years, refDate })',
since: '8.0',
until: '9.0',
});
options = { years: options };
}
const { years = 1, refDate = legacyRefDate } = options;
if (years <= 0) {
throw new FakerError('Years must be greater than 0.');
}
const date = toDate(refDate);
const range = {
min: 1000,
max: years * 365 * 24 * 3600 * 1000,
};
let past = date.getTime();
past -= this.faker.number.int(range); // some time from now to N years ago, in milliseconds
date.setTime(past);
return date;
}
/**
* Generates a random date in the future.
*
* @param options The optional options object.
* @param options.years The range of years the date may be in the future. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.soon()
*
* @example
* faker.date.future() // '2022-11-19T05:52:49.100Z'
* faker.date.future({ years: 10 }) // '2030-11-23T09:38:28.710Z'
* faker.date.future({ years: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2020-12-13T22:45:10.252Z'
*
* @since 8.0.0
*/
future(options?: { years?: number; refDate?: string | Date | number }): Date;
/**
* Generates a random date in the future.
*
* @param years The range of years the date may be in the future. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.soon()
*
* @example
* faker.date.future() // '2022-11-19T05:52:49.100Z'
* faker.date.future(10) // '2030-11-23T09:38:28.710Z'
* faker.date.future(10, '2020-01-01T00:00:00.000Z') // '2020-12-13T22:45:10.252Z'
*
* @since 2.0.1
*
* @deprecated Use `faker.date.future({ years, refDate })` instead.
*/
future(years?: number, refDate?: string | Date | number): Date;
/**
* Generates a random date in the future.
*
* @param options The optional options object.
* @param options.years The range of years the date may be in the future. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to now.
* @param legacyRefDate Deprecated, use `options.refDate` instead.
*
* @see faker.date.soon()
*
* @example
* faker.date.future() // '2022-11-19T05:52:49.100Z'
* faker.date.future({ years: 10 }) // '2030-11-23T09:38:28.710Z'
* faker.date.future({ years: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2020-12-13T22:45:10.252Z'
*
* @since 8.0.0
*/
future(
options?:
| number
| {
years?: number;
refDate?: string | Date | number;
},
legacyRefDate?: string | Date | number
): Date;
future(
options:
| number
| {
years?: number;
refDate?: string | Date | number;
} = {},
legacyRefDate?: string | Date | number
): Date {
if (typeof options === 'number') {
deprecated({
deprecated: 'faker.date.future(years, refDate)',
proposed: 'faker.date.future({ years, refDate })',
since: '8.0',
until: '9.0',
});
options = { years: options };
}
const { years = 1, refDate = legacyRefDate } = options;
if (years <= 0) {
throw new FakerError('Years must be greater than 0.');
}
const date = toDate(refDate);
const range = {
min: 1000,
max: years * 365 * 24 * 3600 * 1000,
};
let future = date.getTime();
future += this.faker.number.int(range); // some time from now to N years later, in milliseconds
date.setTime(future);
return date;
}
/**
* Generates a random date between the given boundaries.
*
* @param options The optional options object.
* @param options.from The early date boundary.
* @param options.to The late date boundary.
*
* @example
* faker.date.between({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z' }) // '2026-05-16T02:22:53.002Z'
*
* @since 8.0.0
*/
between(options: {
from: string | Date | number;
to: string | Date | number;
}): Date;
/**
* Generates a random date between the given boundaries.
*
* @param from The early date boundary.
* @param to The late date boundary.
*
* @example
* faker.date.between('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z') // '2026-05-16T02:22:53.002Z'
*
* @since 2.0.1
*
* @deprecated Use `faker.date.between({ from, to })` instead.
*/
between(from: string | Date | number, to: string | Date | number): Date;
/**
* Generates a random date between the given boundaries.
*
* @param options The optional options object.
* @param options.from The early date boundary.
* @param options.to The late date boundary.
* @param legacyTo Deprecated, use `options.to` instead.
*
* @example
* faker.date.between({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z' }) // '2026-05-16T02:22:53.002Z'
*
* @since 8.0.0
*/
between(
options:
| string
| Date
| number
| {
from: string | Date | number;
to: string | Date | number;
},
legacyTo?: string | Date | number
): Date;
between(
options:
| string
| Date
| number
| {
from: string | Date | number;
to: string | Date | number;
},
legacyTo?: string | Date | number
): Date {
if (typeof options !== 'object' || options instanceof Date) {
deprecated({
deprecated: 'faker.date.between(from, to)',
proposed: 'faker.date.between({ from, to })',
since: '8.0',
until: '9.0',
});
options = { from: options, to: legacyTo };
}
const { from, to } = options;
const fromMs = toDate(from).getTime();
const toMs = toDate(to).getTime();
const dateOffset = this.faker.number.int(toMs - fromMs);
return new Date(fromMs + dateOffset);
}
/**
* Generates random dates between the given boundaries.
*
* @param options The optional options object.
* @param options.from The early date boundary.
* @param options.to The late date boundary.
* @param options.count The number of dates to generate. Defaults to `3`.
*
* @example
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z' })
* // [
* // 2022-07-02T06:00:00.000Z,
* // 2024-12-31T12:00:00.000Z,
* // 2027-07-02T18:00:00.000Z
* // ]
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: 2 })
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
*
* @since 8.0.0
*/
betweens(options: {
from: string | Date | number;
to: string | Date | number;
count?: number;
}): Date[];
/**
* Generates random dates between the given boundaries.
*
* @param from The early date boundary.
* @param to The late date boundary.
* @param count The number of dates to generate. Defaults to `3`.
*
* @example
* faker.date.betweens('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z')
* // [
* // 2022-07-02T06:00:00.000Z,
* // 2024-12-31T12:00:00.000Z,
* // 2027-07-02T18:00:00.000Z
* // ]
* faker.date.betweens('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z', 2)
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
*
* @since 5.4.0
*
* @deprecated Use `faker.date.betweens({ from, to, count })` instead.
*/
betweens(
from: string | Date | number,
to: string | Date | number,
count?: number
): Date[];
/**
* Generates random dates between the given boundaries.
*
* @param options The optional options object.
* @param options.from The early date boundary.
* @param options.to The late date boundary.
* @param options.count The number of dates to generate. Defaults to `3`.
* @param legacyTo Deprecated, use `options.to` instead.
* @param legacyCount Deprecated, use `options.count` instead.
*
* @example
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z' })
* // [
* // 2022-07-02T06:00:00.000Z,
* // 2024-12-31T12:00:00.000Z,
* // 2027-07-02T18:00:00.000Z
* // ]
* faker.date.betweens({ from: '2020-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z', count: 2 })
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
*
* @since 8.0.0
*/
betweens(
options:
| string
| Date
| number
| {
from: string | Date | number;
to: string | Date | number;
count?: number;
},
legacyTo?: string | Date | number,
legacyCount?: number
): Date[];
betweens(
options:
| string
| Date
| number
| {
from: string | Date | number;
to: string | Date | number;
count?: number;
},
legacyTo?: string | Date | number,
legacyCount: number = 3
): Date[] {
if (typeof options !== 'object' || options instanceof Date) {
deprecated({
deprecated: 'faker.date.betweens(from, to, count)',
proposed: 'faker.date.betweens({ from, to, count })',
since: '8.0',
until: '9.0',
});
options = { from: options, to: legacyTo, count: legacyCount };
}
const { from, to, count = 3 } = options;
return Array.from({ length: count }, () => this.between({ from, to })).sort(
(a, b) => a.getTime() - b.getTime()
);
}
/**
* Generates a random date in the recent past.
*
* @param options The optional options object.
* @param options.days The range of days the date may be in the past. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.past()
*
* @example
* faker.date.recent() // '2022-02-04T02:09:35.077Z'
* faker.date.recent({ days: 10 }) // '2022-01-29T06:12:12.829Z'
* faker.date.recent({ days: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2019-12-27T18:11:19.117Z'
*
* @since 8.0.0
*/
recent(options?: { days?: number; refDate?: string | Date | number }): Date;
/**
* Generates a random date in the recent past.
*
* @param days The range of days the date may be in the past. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.past()
*
* @example
* faker.date.recent() // '2022-02-04T02:09:35.077Z'
* faker.date.recent(10) // '2022-01-29T06:12:12.829Z'
* faker.date.recent(10, '2020-01-01T00:00:00.000Z') // '2019-12-27T18:11:19.117Z'
*
* @since 2.0.1
*
* @deprecated Use `faker.date.recent({ days, refDate })` instead.
*/
recent(days?: number, refDate?: string | Date | number): Date;
/**
* Generates a random date in the recent past.
*
* @param options The optional options object.
* @param options.days The range of days the date may be in the past. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to now.
* @param legacyRefDate Deprecated, use `options.refDate` instead.
*
* @see faker.date.past()
*
* @example
* faker.date.recent() // '2022-02-04T02:09:35.077Z'
* faker.date.recent({ days: 10 }) // '2022-01-29T06:12:12.829Z'
* faker.date.recent({ days: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2019-12-27T18:11:19.117Z'
*
* @since 8.0.0
*/
recent(
options?: number | { days?: number; refDate?: string | Date | number },
legacyRefDate?: string | Date | number
): Date;
recent(
options: number | { days?: number; refDate?: string | Date | number } = {},
legacyRefDate?: string | Date | number
): Date {
if (typeof options === 'number') {
deprecated({
deprecated: 'faker.date.recent(days, refDate)',
proposed: 'faker.date.recent({ days, refDate })',
since: '8.0',
until: '9.0',
});
options = { days: options };
}
const { days = 1, refDate = legacyRefDate } = options;
if (days <= 0) {
throw new FakerError('Days must be greater than 0.');
}
const date = toDate(refDate);
const range = {
min: 1000,
max: days * 24 * 3600 * 1000,
};
let future = date.getTime();
future -= this.faker.number.int(range); // some time from now to N days ago, in milliseconds
date.setTime(future);
return date;
}
/**
* Generates a random date in the near future.
*
* @param options The optional options object.
* @param options.days The range of days the date may be in the future. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.future()
*
* @example
* faker.date.soon() // '2022-02-05T09:55:39.216Z'
* faker.date.soon({ days: 10 }) // '2022-02-11T05:14:39.138Z'
* faker.date.soon({ days: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2020-01-01T02:40:44.990Z'
*
* @since 8.0.0
*/
soon(options?: { days?: number; refDate?: string | Date | number }): Date;
/**
* Generates a random date in the near future.
*
* @param days The range of days the date may be in the future. Defaults to `1`.
* @param refDate The date to use as reference point for the newly generated date. Defaults to now.
*
* @see faker.date.future()
*
* @example
* faker.date.soon() // '2022-02-05T09:55:39.216Z'
* faker.date.soon(10) // '2022-02-11T05:14:39.138Z'
* faker.date.soon(10, '2020-01-01T00:00:00.000Z') // '2020-01-01T02:40:44.990Z'
*
* @since 5.0.0
*
* @deprecated Use `faker.date.soon({ days, refDate })` instead.
*/
soon(days?: number, refDate?: string | Date | number): Date;
/**
* Generates a random date in the near future.
*
* @param options The optional options object.
* @param options.days The range of days the date may be in the future. Defaults to `1`.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to now.
* @param legacyRefDate Deprecated, use `options.refDate` instead.
*
* @see faker.date.future()
*
* @example
* faker.date.soon() // '2022-02-05T09:55:39.216Z'
* faker.date.soon({ days: 10 }) // '2022-02-11T05:14:39.138Z'
* faker.date.soon({ days: 10, refDate: '2020-01-01T00:00:00.000Z' }) // '2020-01-01T02:40:44.990Z'
*
* @since 8.0.0
*/
soon(
options?: number | { days?: number; refDate?: string | Date | number },
legacyRefDate?: string | Date | number
): Date;
soon(
options: number | { days?: number; refDate?: string | Date | number } = {},
legacyRefDate?: string | Date | number
): Date {
if (typeof options === 'number') {
deprecated({
deprecated: 'faker.date.soon(days, refDate)',
proposed: 'faker.date.soon({ days, refDate })',
since: '8.0',
until: '9.0',
});
options = { days: options };
}
const { days = 1, refDate = legacyRefDate } = options;
if (days <= 0) {
throw new FakerError('Days must be greater than 0.');
}
const date = toDate(refDate);
const range = {
min: 1000,
max: days * 24 * 3600 * 1000,
};
let future = date.getTime();
future += this.faker.number.int(range); // some time from now to N days later, in milliseconds
date.setTime(future);
return date;
}
/**
* Returns a random name of a month.
*
* @param options The optional options to use.
* @param options.abbr Whether to return an abbreviation. Defaults to `false`.
* @param options.context Whether to return the name of a month in the context of a date. In the default `en` locale this has no effect, however, in other locales like `fr` or `ru`, this may affect grammar or capitalization, for example `'январь'` with `{ context: false }` and `'января'` with `{ context: true }` in `ru`. Defaults to `false`.
*
* @example
* faker.date.month() // 'October'
* faker.date.month({ abbr: true }) // 'Feb'
* faker.date.month({ context: true }) // 'June'
* faker.date.month({ abbr: true, context: true }) // 'Sep'
*
* @since 3.0.1
*/
month(options: { abbr?: boolean; context?: boolean } = {}): string {
const { abbr = false, context = false } = options;
const source = this.faker.definitions.date.month;
let type: keyof DateEntryDefinition;
if (abbr) {
if (context && source['abbr_context'] != null) {
type = 'abbr_context';
} else {
type = 'abbr';
}
} else if (context && source['wide_context'] != null) {
type = 'wide_context';
} else {
type = 'wide';
}
return this.faker.helpers.arrayElement(source[type]);
}
/**
* Returns a random day of the week.
*
* @param options The optional options to use.
* @param options.abbr Whether to return an abbreviation. Defaults to `false`.
* @param options.context Whether to return the day of the week in the context of a date. In the default `en` locale this has no effect, however, in other locales like `fr` or `ru`, this may affect grammar or capitalization, for example `'Lundi'` with `{ context: false }` and `'lundi'` with `{ context: true }` in `fr`. Defaults to `false`.
*
* @example
* faker.date.weekday() // 'Monday'
* faker.date.weekday({ abbr: true }) // 'Thu'
* faker.date.weekday({ context: true }) // 'Thursday'
* faker.date.weekday({ abbr: true, context: true }) // 'Fri'
*
* @since 3.0.1
*/
weekday(options: { abbr?: boolean; context?: boolean } = {}): string {
const { abbr = false, context = false } = options;
const source = this.faker.definitions.date.weekday;
let type: keyof DateEntryDefinition;
if (abbr) {
if (context && source['abbr_context'] != null) {
type = 'abbr_context';
} else {
type = 'abbr';
}
} else if (context && source['wide_context'] != null) {
type = 'wide_context';
} else {
type = 'wide';
}
return this.faker.helpers.arrayElement(source[type]);
}
/**
* Returns a random birthdate.
*
* @param options The options to use to generate the birthdate. If no options are set, an age between 18 and 80 (inclusive) is generated.
* @param options.min The minimum age or year to generate a birthdate.
* @param options.max The maximum age or year to generate a birthdate.
* @param options.refDate The date to use as reference point for the newly generated date. Defaults to `now`.
* @param options.mode The mode to generate the birthdate. Supported modes are `'age'` and `'year'` .
*
* There are two modes available `'age'` and `'year'`:
* - `'age'`: The min and max options define the age of the person (e.g. `18` - `42`).
* - `'year'`: The min and max options define the range the birthdate may be in (e.g. `1900` - `2000`).
*
* Defaults to `year`.
*
* @example
* faker.date.birthdate() // 1977-07-10T01:37:30.719Z
* faker.date.birthdate({ min: 18, max: 65, mode: 'age' }) // 2003-11-02T20:03:20.116Z
* faker.date.birthdate({ min: 1900, max: 2000, mode: 'year' }) // 1940-08-20T08:53:07.538Z
*
* @since 7.0.0
*/
birthdate(
options: {
min?: number;
max?: number;
mode?: 'age' | 'year';
refDate?: string | Date | number;
} = {}
): Date {
const mode = options.mode === 'age' ? 'age' : 'year';
const refDate = toDate(options.refDate);
const refYear = refDate.getUTCFullYear();
// If no min or max is specified, generate a random date between (now - 80) years and (now - 18) years respectively
// So that people can still be considered as adults in most cases
// Convert to epoch timestamps
let min: number;
let max: number;
if (mode === 'age') {
min = new Date(refDate).setUTCFullYear(refYear - (options.max ?? 80) - 1);
max = new Date(refDate).setUTCFullYear(refYear - (options.min ?? 18));
} else {
// Avoid generating dates the first and last date of the year
// to avoid running into other years depending on the timezone.
min = new Date(Date.UTC(0, 0, 2)).setUTCFullYear(
options.min ?? refYear - 80
);
max = new Date(Date.UTC(0, 11, 30)).setUTCFullYear(
options.max ?? refYear - 18
);
}
if (max < min) {
throw new FakerError(`Max ${max} should be larger then min ${min}.`);
}
return new Date(this.faker.number.int({ min, max }));
}
}
|