Display date in Mon dd yyyy format

时间:2019-01-18 19:13:06

标签: angular typescript

I am getting value of Date of birth from my API. i need to display that in 3 different text box like jan ,01 and 2000.

How do I try that in TS.

I have tried storing the date of birth field in Date variable. but it gives value like Sat Jan 01 2000 00:00:00 GMT-0500. Instead of that i need three values Jan 01 and 2000.

I tried Date pipe which gives me date like Jan-01-2000.

 this.newDate=new Date(this.patient.dateOfbirth);

3 个答案:

答案 0 :(得分:0)

检查Ionic DateTime组件 https://ionicframework.com/docs/api/components/datetime/DateTime/

您可以这样做:

代码

dateToShow : any;

this.newDate=new Date(this.patient.dateOfbirth);
dateToShow = newDate.toISOString();

模板

<ion-datetime displayFormat="MMM/DD/YYYY" [(ngModel)]="dateToShow"></ion-datetime>

答案 1 :(得分:0)

请尝试使用管道

有一些预定义的,例如

'mediumDate':等同于'MMM d,y'(2015年6月15日)。 'longDate':等同于'MMMM d,y'(2015年6月15日)。 'fullDate':等同于'EEEE,MMMM d,y'(2015年6月15日,星期一)

使用: {{dateObj |日期:'mediumDate'}}

如果基本不适合您,可以添加Costum管。

{{值|管道:'args'}}

https://angular.io/api/common/DatePipe

答案 2 :(得分:0)

如果要在输入字段中显示日期,建议您像下面这样在类本身中解析日期,并在输入字段中使用它:

  1. 在一年中,您可以:newDate.getFullYear()
  2. 对于月份,创建月份名称数组,例如:let monthNames = ['Jan', 'Feb', ..., 'Dec'],从日期获取月份索引,并使用该名称获取月份名称monthNames[newDate.getMonth()]
  3. 对于日期,newDate.getDate()

如果您只想在HTML模板中显示日期,则可以使用Angular Datepipe,如下所示:

  1. 一年{{newDate | date:'yyyy'}}
  2. 每月{{newDate | date:'MMM'}}
  3. 对于日期,{{newDate | date:'dd'}}