Data javascript format

Asked

Viewed 94 times

1

I was looking at the ISO8601 that talks about the format of string passed as argument for the date object. On this site, the date returned is yesterday. inserir a descrição da imagem aqui

I’m passing the value '2017-08-03' to the date object and it returns me yesterday’s date, see:

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

Because in the jsfiddle different result occurs?

Wed Aug 02 2017 21:00:00 GMT-0300 (Hora oficial do Brasil)

1 answer

-1


Confusion happens only when using alert, if we trade for console.log everything is right. The difference is that the alert flame toString to print the contents of some object, more or less 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 (Official Time in Brazil)

Wrong too. But if we invoke toISOString:

2017-08-03T00:00:00.000Z

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

  • Some fan came out distributing anonymous down votes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.