diff --git a/package.json b/package.json index 3f088d3ce..153e5c1a4 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "rimraf": "^3.0.0", "rollup": "^0.47.0", "rollup-plugin-babel": "^4.0.1", + "timezone-mock": "^1.3.6", "uglify-js": "^3.0.19" }, "scripts": { diff --git a/src/lib/isDate.js b/src/lib/isDate.js index 9f1c6926b..f0b28917a 100644 --- a/src/lib/isDate.js +++ b/src/lib/isDate.js @@ -65,7 +65,19 @@ export default function isDate(input, options) { } } - return new Date(`${fullYear}-${dateObj.m}-${dateObj.d}`).getDate() === +dateObj.d; + let month = dateObj.m; + + if (dateObj.m.length === 1) { + month = `0${dateObj.m}`; + } + + let day = dateObj.d; + + if (dateObj.d.length === 1) { + day = `0${dateObj.d}`; + } + + return new Date(`${fullYear}-${month}-${day}T00:00:00.000Z`).getUTCDate() === +dateObj.d; } if (!options.strictMode) { diff --git a/test/validators.test.js b/test/validators.test.js index 6bf812d15..8fa0f04ae 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -1,5 +1,6 @@ import assert from 'assert'; import fs from 'fs'; +import timezone_mock from 'timezone-mock'; import { format } from 'util'; import vm from 'vm'; import validator from '../src/index'; @@ -13054,6 +13055,17 @@ describe('Validators', () => { '29.02.2020', ], }); + // emulating Pacific time zone offset & time + // which could potentially result in UTC conversion issues + timezone_mock.register('US/Pacific'); + test({ + validator: 'isDate', + valid: [ + new Date(2016, 2, 29), + '2017-08-04', + ], + }); + timezone_mock.unregister(); }); it('should validate time', () => { test({