使用primeng日历时如何格式化日期?

How to format date when using primeng calendar?

Prime NG p-calendar组件如何只获取年月日?当我做 console.log 我的变量时,我得到了这些值。它显示了这个值:

Date {Wed Feb 01 2017 00:00:00 GMT+0700 (SE Asia Standard Time)}

所以我不想要那么久,我只需要 01/02/2017,我不需要其他的。那么我怎样才能得到日期、月份和年份格式呢?因为我已经这样做了:

这是我的 html:

这是我的 component.ts :

export class report {

  public date: any;


  constructor() {

  }

  public proses(date) {

    console.log(date);

  }

}

您可以像这样使用纯 JavaScript 日期函数来执行此操作:

let day = date.getDate();
let month = date.getMonth() + 1; // add 1 because months are indexed from 0
let year = date.getFullYear();

console.log(day + '/' + month + '/' + year);

另一种方法是像这样使用 toLocaleDateString() 方法:

let newDate = date.toLocaleDateString();
console.log(newDate);

或者您可以使用一些库,例如 moment.js,您可以按以下方式使用它:

let newDate = moment(date).format('DD/MM/YYYY').toString();
console.log(newDate();

两种解决方案都将控制台记录相同的字符串。

import {DatePipe} from '@angular/common';

datePipe.transform(selectedDate, formatInWhichYouWantOutput)