Date format javascript

1

I was looking at the ISO8601 that talks about the format of string passed as argument to object date . On this site, the date returned is from yesterday.

I'mpassingthe'2017-08-03'valuetothedateobjectanditreturnsmethedatefromyesterday,see:

data = '2017-08-03'
console.log(new Date(data));

Why does not the jsfiddle have a different result?

Wed Aug 02 2017 21:00:00 GMT-0300 (Hora oficial do Brasil)
    
asked by anonymous 03.08.2017 / 15:04

2 answers

-1

A quick explanation.

w3schools:

getDay() -  Returns the day of the week (from 0-6)
  

As you can see, it returns 0-6 so this difference   you quoted happens.

This happens:

getMonth() - Returns the month (from 0-11)

So always count 0 instead of 1

    
03.08.2017 / 15:23
-1

Confusion happens only when using alert , if we change to console.log everything is right. The difference is that alert calls toString to print the contents of some object, something like this:

data = '2017-08-03'
console.log(new Date(data).toString());

The result will be:

  

Wed Aug 02 2017 21:00:00 GMT-0300 (Brazil's official time)

Wrong too. But if we invoke toISOString :

  

2017-08-03T00: 00: 00ZZ

The important thing is that the date object is being created correctly based on string last.

    
03.08.2017 / 15:26