diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index d7a0f12a017..7156bf2248e 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -949,6 +949,12 @@ export class DateModule { refDate?: string | Date | number; } = {} ): Date { + if (options.max < options.min) { + throw new FakerError( + `Max ${options.max} should be larger than or equal to min ${options.min}.` + ); + } + const mode = options.mode === 'age' ? 'age' : 'year'; const refDate = toDate(options.refDate, this.faker.defaultRefDate); const refYear = refDate.getUTCFullYear(); @@ -973,10 +979,6 @@ export class DateModule { ); } - if (max < min) { - throw new FakerError(`Max ${max} should be larger then min ${min}.`); - } - return new Date(this.faker.number.int({ min, max })); } } diff --git a/test/date.spec.ts b/test/date.spec.ts index b0eec16c9b4..af55508c8bf 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -567,7 +567,7 @@ describe('date', () => { faker.date.birthdate({ min, max, mode: 'year' }) ).toThrow( new FakerError( - `Max 662515200000 should be larger then min 946771200000.` + `Max 1990 should be larger than or equal to min 2000.` ) ); });