Angular Get Current Date and Time Example
Hi,
I am going to explain you example of angular get current date and time. i would like to show you how to get current date and time in angular. this example will help you how to get current date in angular 12. In this article, we will implement a get current time in angular. Let's get started with get current date in angular.
We can get current date and time in 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.
In this example we will use Date class and DatePipe for getting current date and time in angular app.
Let's see bellow example code here:
src/app/app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { DatePipe } from '@angular/common';
import { AppComponent } from './app.component';
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [AppComponent],
providers: [DatePipe],
bootstrap: [AppComponent],
})
export class AppModule {}
src/app/app.component.ts
import { Component, VERSION } from '@angular/core';
import { DatePipe } from '@angular/common';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
constructor(public datepipe: DatePipe){
let currentDateTime =this.datepipe.transform((new Date), 'MM/dd/yyyy h:mm:ss');
console.log(currentDateTime);
}
}
Output:
10/21/2021 9:38:59
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular Material Multi Select Dropdown Example
- Angular Image Upload with Progress Bar Example
- Angular Ngclass with Ternary Operator Example
- Angular 12 Routing Tutorial Example
- Multiple File Upload in Angular 12 Example
- Angular Convert HTML to PDF File Example
- Angular Google Maps with Places Search Example
- How to use Bootstrap Datepicker in Angular?
- Angular 8 Multiple File Upload Example
- How to Redirect to Another Page in Angular?
- How to Get Current URL in Angular?