Why does this give me september 30 rather than oct 1?
var dob = new Date("1999-10-01")
console.log(dob.toString())
Why does this give me september 30 rather than oct 1?
var dob = new Date("1999-10-01")
console.log(dob.toString())
You are creating a Date new Date("1999-10-01")
and parsing it with the method toString()
which is using your local timezone:
var dob = new Date("1999-10-01")
console.log(dob)
console.log(dob.toISOString())
console.log('My local time is different!')
console.log(dob.toLocaleString('es-AR', { timeZone: 'America/Buenos_Aires', timeZoneName: 'long'}))
console.log('Your local time is different?')
console.log(dob.toString())
The format you are using is a subset of ISO 8601
When no timezone designator is specified offset Zulu (UTC) is implied in the date constructor.
http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
All numbers must be base 10. If the MM or DD fields are absent “01” is used as the value. If the HH, mm, or ss fields are absent “00” is used as the value and the value of an absent sss field is “000”. The value of an absent time zone offset is “Z”.
In other words, the format that you are using is valid and it denotes a date-time in UTC. What you are seeing in the console is that time represented in your timezone.
const date = new Date("1999-10-01");
console.log(date.toLocaleDateString('ar-EG'));
console.log(date.toString());
console.log(date.toISOString());