Add days to a date in javascript

shiftDay = 10
var myDate = new Date();
myDate.setDate(myDate.getUTCDate() + shiftDay)
day   = myDate.getUTCDate()
month = myDate.getUTCMonth()*1 + 1
year  = myDate.getUTCFullYear()

This code is quite easy:

  • shiftDay hold the number of days you want to add, if you need to go backwards you can set it to a negative value.
  • myDate is set to the current date
  • At the end we will have the data we need in day, month and year.
  • getUTCMonth return the current month shifted back by one (January is 0, February is 1 and so on) that's why we add 1. We multiply the value returned by getUTCMonth by one to cast it to integer.

Leave a Reply

Yes !