Set date 7 days/a week ago

Asked

Viewed 505 times

2

I would like to get today’s date, and subtract it from 7 days. Remember that if we are on the 2nd for example, you should subtract 1 from the month. The same applies to the year.

Unfortunately, I only found examples that refer to a specific day, and not subtract an n of days.

1 answer

4


Get date from a week/month/year ago:

var uma_semana = new Date();
uma_semana.setDate(uma_semana.getDate() - 7);

var um_mes = new Date();
um_mes.setMonth(um_mes.getMonth() - 1);

var um_ano = new Date();
um_ano.setFullYear(um_ano.getFullYear() - 1);

console.log(uma_semana);
console.log(um_mes);
console.log(um_ano);
  • um_mes.getDate() - 30 and um_ano.getDate() - 365 is not perfect. There are months and years with different durations.

  • You’re absolutely right, I thought it was dated 30 days ago. I’ll correct

Browser other questions tagged

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