Get some days ago date using JavaScript

 

function someDaysAddSub(daysDiff, date = new Date(), before = true) {

  let daysAgo = new Date(date.getTime());
  
  if(before){
  	daysAgo.setDate(date.getDate() - daysDiff);
  }else{
  	daysAgo.setDate(date.getDate() + daysDiff);
  }

  return daysAgo;
}

let newdate = new Date();
let daysDiff = 5

// five days ago
someDaysAddSub (newdate , daysDiff, true)

// five days after
someDaysAddSub (newdate , daysDiff, false)

Tags:

Share:

Related posts