Home > Software engineering >  Getting issue while set the previous month date as default to calender Angular
Getting issue while set the previous month date as default to calender Angular

Time:01-24

I am setting the previous month 1st as default in calender and restrict not to go beyound but from current date it is year changing it must go to previous year like current date is 24/1/2022 I want to set default 1/12/2021 I used this peace of code it is not working

.ts file

this.minDate = {
  year: current.getFullYear(),
  month: current.getMonth(),
  day: (new Date(current.getFullYear(), current.getMonth()-1, 1).getDate())
};

.html

 <input  autocomplete="off" [(ngModel)]="startDateVal"
        placeholder="From yyyy-mm-dd" name="dp1"   [minDate]="minDate"  ngbDatepicker #d="ngbDatepicker" (ngModelChange)="dateSelected($event)" >
        <span ></span>

Calender is not working properly.

CodePudding user response:

I tried this solution this worked for me:

 let current = new Date();
  current.setMonth(current.getMonth()-1);
  start.year = current.getFullYear();
  start.month = current.getMonth() 1;
  start.day = (new Date(current.getFullYear(), current.getMonth() 1, 1).getDate())

CodePudding user response:

You need to use https://ng-bootstrap.github.io/#/components/datepicker/overview#date-model NgbDateStruct (an object with fields like year, month, day) instead of the built-in JavaScript Date object. For example, to set a min date you would use:

minValue = {year: 2021, month: 12, day: 1};
  •  Tags:  
  • Related