user14860979
user14860979

Reputation: 127

Rest or add days to date

I need to add or rest dates to today, I'm getting today by this method:

   var today = (new Date()).toISOString().split('T')[0];

this is because I need the yyy-mm-dd format, but also I need get another 2 dates, for example a start date and an end date where:

StartDate should be equals to rest 7 days to today. EndDate should be equals to add 7 days to today.

So in this case

For this I'm using TypesCript

Upvotes: 0

Views: 882

Answers (1)

hoangdv
hoangdv

Reputation: 16147

Just calculate on the Date object.

var today = new Date();

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

var endDate = new Date();
endDate.setDate(today.getDate() + 7);

console.log('TODAY', today.toISOString().split('T')[0]);
console.log('START_DATE', startDate.toISOString().split('T')[0]);
console.log('END_DATE', endDate.toISOString().split('T')[0]);

Upvotes: 1

Related Questions