new Date () in javascript I have one day left

1

I am working with dates, and when I make a date with the new Date () method, I have one day left.

My code:

let prueba = new Date('2018-12-04')
console.log(prueba)

and in the console I have the following date:

Mon Dec 03 2018 21:00:00 GMT-0300 (hora estándar de Argentina)

That is, I want the date to be December 4th and in console I have one day less .....

Thanks in advance ...

    
asked by amblador 03.12.2018 в 22:30
source

3 answers

2

When you create a new Date object using that date format ( yyyy-mm-dd ) it takes 00:00:00 as the default time zone the +0. The date is set as follows: December 4, 2018 at 0hs in area +0.

When you print the browser date in the console, it is set to the time zone you have configured on your computer, in this case GMT-3. When making this adjustment so that you see the date "translated" to your time zone you subtract these three hours of difference and stay with December 3, 2018 at 9:00 pm in zone -3.

The way to make sure the correct date is on Date is to add the time:

var d = new Date("2015-12-04T00:00:00");

By doing so, the browser assumes that the time indicated is in your time zone and automatically adjusts it to store it correctly.

    
answered by 03.12.2018 / 22:59
source
1

I recommend this portion of code to compensate for the difference caused by the time zone that is causing this difference in dates:

let fecha = new Date('2018-12-04')
fecha.setMinutes(fecha.getMinutes() + fecha.getTimezoneOffset())
console.log(fecha)
    
answered by 03.12.2018 в 23:15
0

You are using the ISO date: "2018-12-04" (The International Standard). Depending on your time zone it varies between December 03 and December 04. Although in your case it is Argentina, what you must do is increase it one more day to your date so that it shows you the exact date you want. As I reiterate, it is standard for Javascript.

Reference: link

    
answered by 03.12.2018 в 22:46