Faced such a problem: I want to take the date of the previous day, but when the date is the first day of the year, then the javascript date does not work correctly.

Here is a sample code:

Here I have to return such result day - 31, month - 11 (месяцы из даты начинаются с 0 до 11) и year - 2016 , but my result in the example is such a day - 31, month - 0, year - 217

 var d = new Date(2017, 1, 1); d = new Date(d.getTime() - 86400000); var from_day = d.getDate(); console.log(d.getTime()); console.log(from_day); console.log(d.getMonth()); console.log(d.getFullYear()); 

Closed due to the fact that off-topic participants Alexey Shimansky , andreymal , insolor , kmv , sanmai 20 Sep '17 at 3:59 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Alexey Shimansky, andreymal, insolor, kmv, sanmai
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    So you initially set the date for February 1. - Oleg
  • @Oleg what is most funny, the author himself wrote it in question)) - Alexey Shimansky
  • @ Alexey Shimansky Yeah, I also find it funny. Sometimes you earn. - Oleg
  • @ AlekseyShimansky Thank you, I have already spent an hour for a stupid mistake)))) - Raz Galstyan

2 answers 2

You set the date to February 1st.

 var d = new Date(2017, 0, 1); d = new Date(d.getTime() - 86400000); var from_day = d.getDate(); console.log(d.getTime()); console.log(from_day); console.log(d.getMonth()); console.log(d.getFullYear()); 

Changed the month to 0 and that's it.

    The option with subtraction of 24 hours (or 86400000 ms.) May not always / not work correctly everywhere. For example, on days when the transition from summer time to standard time occurs, there will be 25 hours in days.

    If you need to receive the previous calendar day, then it would be more correct to do so:

     var d = new Date(2017, 1, 1); //1 февраля 2017 d.setDate(d.getDate() - 1); //d изменится на 31 января 2017 

    The setDate function sets the day of the current month of the date, while taking into account the user's racial zone (and the transitions of the given time zone). Moreover, if the day of the month is beyond the number of days in a given month, the month will be changed. So, if setDate pass 0 to the setDate input, then the month will change to the previous one, and the day will be equal to the last day of the previous month. If you pass 29 then the month will change to the next (since in February 2017 only 28 days).

     var d = new Date(2017, 0, 1); //1 января 2017 d.setDate(d.getDate() - 1); // d изменится на 31 декабря 2016