Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - How to show only month and year in ngxbootstrap/datepicker

I'm using ngx bootstrap datepicker in the project

Needed only month and year to be selected like the above image

enter image description here

<input type="text"
         class="form-control"
          (bsValueChange)="modelChanged($event)"
          #dp="bsDatepicker"
          bsDatepicker [(bsValue)]="bsValue"
          [bsConfig]="{ dateInputFormat: 'YYYY-MM-DD' }"
          >

is there any way to config the date-picker like this? or Suggest any alternative date-picker which allow this functionality

like image 995
Aashiq Rathnadas Avatar asked Mar 08 '18 04:03

Aashiq Rathnadas


1 Answers

you should import the following:

import { BsDatepickerConfig, BsDatepickerViewMode } from 'ngx-bootstrap/datepicker';

@Component({
  selector: 'demo-datepicker-min-mode',
  templateUrl: './min-mode.component.html'
})
export class DemoDatepickerMinModeComponent implements OnInit {
  bsValue: Date = new Date(2017, 7);
  minMode: BsDatepickerViewMode = 'month'; // change for month:year

  bsConfig: Partial<BsDatepickerConfig>;

  ngOnInit(): void {
    this.bsConfig = Object.assign({}, {
      minMode : this.minMode
    });
  }
}

and in your HTML file

<div class="row">
  <div class="col-xs-12 col-12 col-md-4 form-group">
    <input type="text"
           class="form-control"
           [bsConfig]="bsConfig"
           #dp="bsDatepicker"
           bsDatepicker [(bsValue)]="bsValue">
  </div>
  <div class="col-xs-12 col-12 col-md-3 form-group">
    <button class="btn btn-success" (click)="dp.toggle()" type="button">Date Picker</button>
  </div>
</div>
<br>
like image 175
Jordi Cuevas Avatar answered Sep 27 '22 21:09

Jordi Cuevas