0

Why does this give me september 30 rather than oct 1?

var dob = new Date("1999-10-01")
console.log(dob.toString())

9
  • because you did not specify a timezone. Commented Sep 24, 2018 at 15:46
  • It's taking it off your local system time. for me it's BST so shows as GMT+1 Commented Sep 24, 2018 at 15:46
  • 1
    You're using a non-standard date format, so you're lucky it gives you a date instance at all. It's probably interpreting your date string as a UTC date/time and you're somewhere a few hours behind UTC. Commented Sep 24, 2018 at 15:46
  • it give me Fri Oct 01 1999 02:00:00 GMT+0200 : maybe it deals with local timezone ? Commented Sep 24, 2018 at 15:47
  • 1
    @Pointy The format is standard ISO86001 Commented Sep 24, 2018 at 15:54

2 Answers 2

0

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())

Sign up to request clarification or add additional context in comments.

Comments

0

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());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.