Angular Get Current Date and Time Example

By Hardik Savani October 21, 2021 Category : Angular

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 and angular 12.

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...

Tags :