Skip to content

Commit 462c80e

Browse files
authored
fix(date): fix birthdate (#2829)
1 parent 0fe5af8 commit 462c80e

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

src/modules/date/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,10 @@ export class SimpleDateModule extends SimpleModuleBase {
523523

524524
switch (mode) {
525525
case 'age': {
526-
const from = new Date(refDate).setUTCFullYear(refYear - max - 1);
526+
// Add one day to the `from` date to avoid generating the same date as the reference date.
527+
const oneDay = 24 * 60 * 60 * 1000;
528+
const from =
529+
new Date(refDate).setUTCFullYear(refYear - max - 1) + oneDay;
527530
const to = new Date(refDate).setUTCFullYear(refYear - min);
528531

529532
if (from > to) {

test/modules/__snapshots__/date.spec.ts.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ exports[`date > 42 > betweens > with string dates and count 1`] = `
6565
]
6666
`;
6767

68-
exports[`date > 42 > birthdate > with age and refDate 1`] = `1980-07-07T19:06:53.165Z`;
68+
exports[`date > 42 > birthdate > with age and refDate 1`] = `1980-07-08T10:07:32.899Z`;
6969

70-
exports[`date > 42 > birthdate > with age range and refDate 1`] = `1962-12-27T20:14:08.437Z`;
70+
exports[`date > 42 > birthdate > with age range and refDate 1`] = `1962-12-28T11:14:48.171Z`;
7171

72-
exports[`date > 42 > birthdate > with only refDate 1`] = `1963-09-27T06:10:42.813Z`;
72+
exports[`date > 42 > birthdate > with only refDate 1`] = `1963-09-27T21:11:22.547Z`;
7373

7474
exports[`date > 42 > birthdate > with year 1`] = `2000-05-16T22:59:36.655Z`;
7575

@@ -189,11 +189,11 @@ exports[`date > 1211 > betweens > with string dates and count 1`] = `
189189
]
190190
`;
191191

192-
exports[`date > 1211 > birthdate > with age and refDate 1`] = `1981-01-26T13:16:31.426Z`;
192+
exports[`date > 1211 > birthdate > with age and refDate 1`] = `1981-01-26T14:59:27.285Z`;
193193

194-
exports[`date > 1211 > birthdate > with age range and refDate 1`] = `1996-10-13T01:44:07.954Z`;
194+
exports[`date > 1211 > birthdate > with age range and refDate 1`] = `1996-10-13T03:27:03.813Z`;
195195

196-
exports[`date > 1211 > birthdate > with only refDate 1`] = `1998-08-21T21:24:31.101Z`;
196+
exports[`date > 1211 > birthdate > with only refDate 1`] = `1998-08-21T23:07:26.960Z`;
197197

198198
exports[`date > 1211 > birthdate > with year 1`] = `2000-12-04T01:16:03.291Z`;
199199

@@ -311,11 +311,11 @@ exports[`date > 1337 > betweens > with string dates and count 1`] = `
311311
]
312312
`;
313313

314-
exports[`date > 1337 > birthdate > with age and refDate 1`] = `1980-05-27T14:46:44.794Z`;
314+
exports[`date > 1337 > birthdate > with age and refDate 1`] = `1980-05-28T08:29:25.862Z`;
315315

316-
exports[`date > 1337 > birthdate > with age range and refDate 1`] = `1956-02-15T21:16:37.850Z`;
316+
exports[`date > 1337 > birthdate > with age range and refDate 1`] = `1956-02-16T14:59:18.918Z`;
317317

318-
exports[`date > 1337 > birthdate > with only refDate 1`] = `1956-08-25T03:56:58.153Z`;
318+
exports[`date > 1337 > birthdate > with only refDate 1`] = `1956-08-25T21:39:39.221Z`;
319319

320320
exports[`date > 1337 > birthdate > with year 1`] = `2000-04-06T02:45:32.287Z`;
321321

test/modules/date.spec.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,19 @@ describe('date', () => {
528528
});
529529

530530
describe('birthdate', () => {
531+
function calculateAge(birthdate: Date, refDate: Date): number {
532+
let age = refDate.getFullYear() - birthdate.getFullYear();
533+
if (
534+
refDate.getMonth() < birthdate.getMonth() ||
535+
(refDate.getMonth() === birthdate.getMonth() &&
536+
refDate.getDate() < birthdate.getDate())
537+
) {
538+
age--;
539+
}
540+
541+
return age;
542+
}
543+
531544
it('returns a random birthdate', () => {
532545
const birthdate = faker.date.birthdate();
533546
expect(birthdate).toBeInstanceOf(Date);
@@ -577,8 +590,8 @@ describe('date', () => {
577590
const value = birthdate.valueOf();
578591
const refDateValue = refDate.valueOf();
579592
expect(value).toBeLessThanOrEqual(refDateValue);
580-
const deltaDate = new Date(refDateValue - value);
581-
expect(deltaDate.getUTCFullYear() - 1970).toBe(21);
593+
const age = calculateAge(birthdate, refDate);
594+
expect(age).toBe(21);
582595
});
583596

584597
it('returns a random birthdate between two ages', () => {
@@ -592,9 +605,9 @@ describe('date', () => {
592605
const value = birthdate.valueOf();
593606
const refDateValue = refDate.valueOf();
594607
expect(value).toBeLessThanOrEqual(refDateValue);
595-
const deltaDate = new Date(refDateValue - value);
596-
expect(deltaDate.getUTCFullYear() - 1970).toBeGreaterThanOrEqual(21);
597-
expect(deltaDate.getUTCFullYear() - 1970).toBeLessThanOrEqual(22);
608+
const age = calculateAge(birthdate, refDate);
609+
expect(age).toBeGreaterThanOrEqual(21);
610+
expect(age).toBeLessThanOrEqual(22);
598611
});
599612

600613
it.each(['min', 'max', 'mode'] as const)(

0 commit comments

Comments
 (0)