Capturing Date Retroactive

0

I have the following question. I have to capture two dates, one being today and the other being 15 days behind. To get the date of the day I did the following.

function dateFormat() {
var initialDate = new Date(),
day = initialDate.getDate(),
month = initialDate.getMonth() + 1,
year = initialDate.getFullYear();
return (year * 10000) + (month * 100) + day; }

I can get today's date without problems, but how should I get a date 15 days ago. Being that there is the difference of days between the months.

Note that the return value in the function is due to the way I should send this date to the server, as it should be YYYYMMDD .

    
asked by anonymous 12.12.2017 / 21:06

1 answer

0

You can use the setDate function to set the date by decreasing its 15 days, see example

function dateFormat() {
  var initialDate = new Date();
  initialDate.setDate(initialDate.getDate() - 15);
  day = initialDate.getDate();
  month = initialDate.getMonth() + 1;
  year = initialDate.getFullYear();
  return (year * 10000) + (month * 100) + day; 
}
    
12.12.2017 / 21:12