How to Bind Current Date to Angular DatePicker?
Binding the current date to a DatePicker is a common requirement in Angular applications. This guide demonstrates how to achieve this using Angular and the Syncfusion® Angular DatePicker component.
Prerequisites
-
Review the system requirements for developing, testing, and deploying applications using Syncfusion® Angular UI Components.
-
For creating an Angular application with Syncfusion® DatePicker, refer to the Getting Started with Angular DatePicker component guide.
Implementation
The value
property of the Syncfusion® Angular DatePicker enables binding the current system date using JavaScript’s new Date()
constructor.
app.component.html
<ejs-datepicker [value]="currentDate" placeholder="Select a date"></ejs-datepicker>
app.component.ts
import { DatePickerModule } from '@syncfusion/ej2-angular-calendars';
@Component({
selector: 'app-root',
styleUrls: ['app.component.css'],
templateUrl: 'app.component.html',
encapsulation: ViewEncapsulation.None,
standalone: true,
imports: [DatePickerModule],
})
export class AppComponent {
public currentDate: Date = new Date(); // Binds today's date
}
Key Points
- The
value
property accepts a Date object for date binding - Using
new Date()
automatically sets the current system date
Preview
The DatePicker component will display with the current date pre-selected, providing users with immediate context while allowing them to modify the selection as needed.