ItSolutionStuff.com

Angular Material Datepicker Example

By Hardik Savani • May 2, 2024
Angular

Today, our leading topic is how to use datepicker in angular and how to use material datepicker in angular. so basically i will give you example of angular material datepicker step by step.

You can easily use material datepicker with angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17.

Datepicker is a primary requirement of project. we almost require to use datepicker in angular app. angular provide material design and they provide how to use datepicker in angular application. i will show you here step by step how you can use material datepicker in angular.

In this example, we will add material design theme and then import some dependency module of datepicker. then we will simply write code of datepicker from angular document.

So, let's see bellow example from here:

Step 1: Create New App

You can easily create your angular app using bellow command:

ng new myDatepicker

Step 2: Add Material Design

Now in this step, we need to just install material design theme in our angular application. so let's add as like bellow:

ng add @angular/material

Cmd like bellow:

Installing packages for tooling via npm.

Installed packages for tooling via npm.

? Choose a prebuilt theme name, or "custom" for a custom theme: Indigo/Pink

[ Preview: https://material.angular.io?theme=indigo-pink ]

? Set up HammerJS for gesture recognition? Yes

? Set up browser animations for Angular Material? Yes

Step 3: Import Module

In third step, we need to import some dependency like MatDatepickerModule, MatNativeDateModule, MatInputModule. so let's add.

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MatDatepickerModule, MatInputModule, MatNativeDateModule } from '@angular/material';

@NgModule({

declarations: [

AppComponent

],

imports: [

BrowserModule,

BrowserAnimationsModule,

MatInputModule,

MatDatepickerModule,

MatNativeDateModule,

],

providers: [],

bootstrap: [AppComponent]

})

export class AppModule { }

Step 4: Use Datepicker

Now in view file, we will write code of input element with datepicker as like bellow:

src/app/app.component.html

<h1>How to install material design in angular - ItSolutionStuff.com</h1>

<mat-form-field>

<input matInput [matDatepicker]="picker" placeholder="Choose a date">

<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>

<mat-datepicker #picker></mat-datepicker>

</mat-form-field>

Now we are ready to run our example, you can run by following command:

ng serve

you will see layout as bellow:

I hope it can help you...

Tags: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Angular FormGroup Example Tutorial

Read Now →

Angular Toggle a Div on Button Click Example

Read Now →

Angular Ng-Container Example Tutorial

Read Now →

Angular Service with Httpclient Example

Read Now →

How to Create Reusable Components in Angular 10/9/8?

Read Now →

How to Get User Agent in Angular?

Read Now →

Angular Http Post Request Example

Read Now →

Angular Get Current Route Name Example

Read Now →

Angular Change Date Format in Component Example

Read Now →

Reactive Form with Validation in Angular 8

Read Now →