frustrated-dev
frustrated-dev

Reputation: 451

How can we use button to increment and decrement date in angular datepicker

How can we use this arrow button to increment and decrement date in angular datepicker. I am really new to this one I need your help to implement this function. Thanks in advance.

app.component.ts

<button mat-button class="material-icons">keyboard_arrow_left</button>
<button mat-button class="material-icons">keyboard_arrow_right</button>  

<mat-form-field appearance="fill">
  <!-- <mat-label>Verbose datepicker</mat-label> -->
  <input matInput [matDatepicker]="dp" [formControl]="date">
  <mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
  <mat-datepicker #dp></mat-datepicker>
</mat-form-field>

app.component.ts

import { Component } from '@angular/core';
import {FormControl} from '@angular/forms';
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';

import _moment from 'moment';
// tslint:disable-next-line:no-duplicate-imports
import {default as _rollupMoment} from 'moment';

const moment = _rollupMoment || _moment;

// See the Moment.js docs for the meaning of these formats:
// https://momentjs.com/docs/#/displaying/format/
export const MY_FORMATS = {
  parse: {
    dateInput: 'LL',
  },
  display: {
    dateInput: 'LL',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  },
};

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  providers: [
    // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
    // application's root module. We provide it at the component level here, due to limitations of
    // our example generation script.
    {
      provide: DateAdapter,
      useClass: MomentDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    },

    {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
  ],
})
export class AppComponent  {
  date = new FormControl(moment());
}

enter image description here

How can we use this arrow button to increment and decrement date in angular datepicker. I am really new to this one I need your help to implement this function. Thanks in advance.

Upvotes: 1

Views: 1147

Answers (2)

truonghungit
truonghungit

Reputation: 312

The simple way is using setValue() function of FormControl to resolve your issues like this: https://stackblitz.com/edit/angular-px9ayg?file=src/app/datepicker-overview-example.html

Upvotes: 1

Sherif Elmetainy
Sherif Elmetainy

Reputation: 4502

You can do that by setting the value of the date form control.

<button mat-button class="material-icons" (click)="incrementDate()">keyboard_arrow_right</button>

And in typescript code

public incrementDate() {
    // this.date.value gets the current value
    // it is a moment value and we can use add function to increment value
    this.date.setValue(this.date.value.add(1, 'days'));
}

Upvotes: 3

Related Questions