修剪日期格式PrimeNG日历-删除时间戳,角度反应形式 [英] Trim date format PrimeNG calendar - remove timestamp, angular reactive forms

查看:72
本文介绍了修剪日期格式PrimeNG日历-删除时间戳,角度反应形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下内容放入我的反应形式obj 2016-01-01T00:00:00.000Z 但我想要以下 2016-01-01 .

I have the following pushed into my reactive forms obj 2016-01-01T00:00:00.000Z but I want the following 2016-01-01.

没有人知道实现上述目标的内置功能.我在此处搜索了文档,但没有运气.当然,一个 .trim 在回调中可以正常工作,但我想知道是否有人以前做过此事并且知道内置的功能.

Does anyone know of a built in function to achieve the above. I've searched the docs here but no luck. A .trim would of course work fine in a callback, but I'm wondering if anyone has done this before and knows a built in functionality.

谢谢.

更新

为了帮助别人-这就是我如何使用Primng压光机,呈角度的反应形式在以下答案的帮助下解决该问题的方法.使用特定于 primeNg日历(onSelect)回调

To help others - this is how I solved it with the help of the answer below using primeng calender, reactive forms in angular. Using (onSelect) callback specific to primeNg Calendar

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

<p-calendar
    #purchaseDateRef
    (onSelect)="handleSelect(purchaseDateRef)">
</p-calendar>


constructor(
    private datePipe: DatePipe
) {}

handleSelect(event: any) {
  // manipulate date object with help of DatePipe and setValue
  this.form.get('registrationFields.appliance.purchaseDate')
     .setValue(this.datePipe.transform(event.value, 'y.MM.dd')
  );
}

推荐答案

您需要在组件中注入DatePipe,就像

You need to inject DatePipe in your component, as

构造函数(私有datePipe:DatePipe){}

constructor( private datePipe: DatePipe){}

您可以在组件中的任何位置使用转换功能

than you can use wherever in the component the transform function

const formattedDate = this.datePipe.transformer(dateValue,'build_in_date_format')

const formattedDate = this.datePipe.transformer(dateValue, 'build_in_date_format')

有许多内置格式,请参考官方文档 https://angular.io/api/common/DatePipe

there are many build in formats, refer to the official documentation https://angular.io/api/common/DatePipe

这篇关于修剪日期格式PrimeNG日历-删除时间戳,角度反应形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆