How to Get current Time and Date in Angular?

In this blog, we are going to discuss that How to get the Current Time and Date in Angular. Here we will show you the code with an example.How to Get current Time and Date in Angular?

We can get the current date and time in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, and angular 13.

In this example, we will use the Date class and DatePipe for getting the current date and time in the angular app.

You just need to follow the following code and files:

This is the code for app.module file, follow as below:

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 {}

This is the code for app.component file, follow as below:

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 :

02/12/2021 18:47:36

I hope you will follow the above steps and use this in your application.

So, thanks for following this blog, and if you need any help please leave your queries in the comment box we will be happy to help you. How to get data in the root file in Magento 2?expoundsofttechsolution