htmlangulartypescriptdaterangepickerngx-daterangepicker-material

How to change default value in ngx-daterangepicker-material?


I want to change default value of gx-daterangepicker-material, for example for yesterday to last 7days, could anyone know how to set it ?

CODE:

export class CustomRangesComponent implements OnInit {
selected: any;
alwaysShowCalendars: boolean;
ranges: any = {
  'Today': [moment(), moment()],
  'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
  'Last 7 Days': [moment().subtract(6, 'days'), moment()],
  'Last 30 Days': [moment().subtract(29, 'days'), moment()],
  'This Month': [moment().startOf('month'), moment().endOf('month')],
  'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
invalidDates: moment.Moment[] = [moment().add(2, 'days'), moment().add(3, 'days'), moment().add(5, 'days')];

isInvalidDate = (m: moment.Moment) =>  {
  return this.invalidDates.some(d => d.isSame(m, 'day') )
}

constructor() {
  this.alwaysShowCalendars = true;
}


<input type="text"
  ngxDaterangepickerMd
  [(ngModel)]="selected"
  [showCustomRangeLabel]="true"
  [alwaysShowCalendars]="true"
  [ranges]="ranges"
  [linkedCalendars]="true"
  [isInvalidDate] = "isInvalidDate"
  [showClearButton]="true"

  placeholder="Select please.../>

https://fetrarij.github.io/ngx-daterangepicker-material/custom-ranges


Solution

  • In "selected" add the start date and end date of the range you want.

    selected = {
      startDate: dayjs().subtract(1, 'days').set('hours', 0).set('minutes', 0),
      endDate: dayjs().subtract(1, 'days').set('hours', 23).set('minutes', 59)
    };
    

    its for displying yesterday.