"1991-1-1" is not a format supported by ECMAScript so parsing is implementation dependent, it might be parsed as UTC, local or treated as an invalid date. new Date('1991-1-1')
returns an invalid date in Safari at least.
If you just want to add a leading zero to single digit numbers, do that and avoid the vagaries of the built–in parser:
// Using split + join
console.log(
'1991-1-1'.split('-').map(c => (c<10?'0':'') + +c).join('-')
);
// Using replace
console.log(
'1991-1-1'.replace(/\d+/g, c => (c<10?'0':'') + +c)
);
PS
Safari also incorrectly treats 1991-01-01 as local, not UTC, so for users with a negative timezone offset (positive in EMCAScript) new Date('1991-01-01')
returns a date for 1990-12-31. See Why does Date.parse give incorrect results?
If you really must use Date, then manually parse it as UTC and use toISOString. That avoids issues with the built–in parser and timezone offsets.
// Change YYYY-M-D to YYYY-MM-DD
// Separator can be any non-digit character
function reformatDate(s) {
let [y,m,d] = s.split(/\D/);
return new Date(Date.UTC(y, m-1, d)).toISOString().slice(0,10);
}
console.log(reformatDate('1991-1-1'));