How to capture backdate

Asked

Viewed 109 times

0

I have the following question. I have to capture two dates, one being today and the other being 15 days back. 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. Since there is the difference of days between the months.

Note the value of Return function is due to how I should send this date to the server, as it should go as YYYYMMDD.

  • You want to take the date back and do what in function?

  • I have to get the data from a table that changes daily and I’m creating a filter that takes the data from the last 15 days.

1 answer

0

You can use the setDate function to set the date by decreasing your 15 days, see the 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; 
}
  • You’re making a strange return. I did the test like this, day = initialDate.setDate(initialDate.getDate() - 15), and he gave it on the return of the day... 1511814481892

  • I made some changes to the answer, see if it now works for you

  • I was able to solve it differently. I made a dateOffset variable, to count backwards time, based on the hours var dateOffset = (246060*1000) * 15; //15 days var initialDate = new Date(); initialDate.setTime(initialDate.getTime() - dateOffset);

Browser other questions tagged

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