How to get date and time in Angular 4,5,6 and above using DatePipe

后端 未结 3 1636
终归单人心
终归单人心 2021-02-04 09:47

I am working in an angular 4 application, Here I need to get the current Date and Time Using angular DatePipe.

I want to get the date and time

3条回答
  •  长发绾君心
    2021-02-04 10:24

    Will works on Angular 6 or above

    You don't need any 3rd party library. You can format using angular method/util. import formatDate from the common package and pass other data. See the below-given example.

    import { Component } from '@angular/core';
    import {formatDate } from '@angular/common';
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      today= new Date();
      jstoday = '';
      constructor() {
        this.jstoday = formatDate(this.today, 'dd-MM-yyyy hh:mm:ss a', 'en-US', '+0530');
      }
    }
    

    stackblitz: https://stackblitz.com/edit/angular-vjosat?file=src%2Fapp%2Fapp.component.ts

提交回复
热议问题