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

2

11

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

Bulbous answered 8/3, 2018 at 4:51 Comment(3)
Hey @aashiq, are you able to solve this problem? I am also into the same trouble. I also want a view like yours. Can you help me how you solved it?Cayuga
@Cayuga No not yet using ngx datepicker, could achieve if we are using material date picker or elseBulbous
How do we highlight the selected month?Raynell
S
1

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>
Stilbestrol answered 19/11, 2019 at 15:25 Comment(0)
C
0

Currently, there is no way to achieve what you want. That is a very popular request in the ngx-bootstrap repo, by the way.

The valor-software team is implementing this feature right now, and you can try what they've done so far. Check out for the next ngx-bootstrap release. Here's a link to the pull request.

Chirrup answered 5/11, 2018 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.