ShivShankar Namdev
ShivShankar Namdev

Reputation: 298

Angular 8 How to Add days to current date

I want to add days to the current date

I have tried currentdate.getDate() but this dosent work

console.log(formatDate(this.currentUser.businessDate, 'dd-EEE', 'en-Us'));
console.log(formatDate(this.currentUser.businessDate, 'dd-MMM', 'en-Us'));
let a=this.currentBusinesDate.getDate();

If date is 23-Jun and i add 1 date it shoud be 24-Jun

Upvotes: 1

Views: 12162

Answers (4)

Sandun Sameera
Sandun Sameera

Reputation: 432

Had the same problem and after some research I found a reliable method, I'll put my approach for any one who isnt looking for above approaches.

generateValidRange() {
const unit = this.registrationDetailsFormGroup.controls.unit.value;
const duration = this.registrationDetailsFormGroup.controls.duration.value;
const date = new Date();
if (unit !== '' && unit !== null && duration !== '') {
  if (unit.code === duration_unit.days.code) {
    date.setDate(date.getDate() + Number(duration));
  }
  if (unit.code === duration_unit.months.code) {
    date.setDate(date.getMonth() + Number(duration));
  }
  if (unit.code === duration_unit.years.code) {
    date.setDate(date.getFullYear() + Number(duration));
  }
}
return date.getDate() + '/' + date.getMonth() + '/' + date.getFullYear();}

here duration_unit is

`duration_unit={
 days: {code: 'D', name: 'DAYS'},
 months: {code: 'M', name: 'MONTHS'},
 weeks: {code: 'W', name: 'WEEKS'},
 years: {code: 'Y', name: 'YEARS'},
 };`

Hope this will help for someone !! cheers

Upvotes: 0

Vadivel Subramanian
Vadivel Subramanian

Reputation: 458

Try this one

   this.date = new Date('2020-03-26');
   this.date.setDate( this.date.getDate() + 3 );
   var d = new Date(this.date);
   console.log((d.getMonth()+1) + '/' + d.getDate() + '/' + d.getFullYear());

Upvotes: 0

StyrianDev
StyrianDev

Reputation: 239

Use moment.js, that library is going to provide tons of date functions: This thread has explained how to use it with typescript: How to consume the Moment.js TypeScript definition file if my site is already using moment.min.js?

Upvotes: 1

Seba Cherian
Seba Cherian

Reputation: 1793

try this:

date: Date;

ngOnInit() {
  this.date = new Date();
  this.date.setDate( this.date.getDate() + 1 );
}

Upvotes: 5

Related Questions