Angular Date type

 ̄綄美尐妖づ 提交于 2021-02-19 08:06:05

问题


From my angular interface date value coming like this.

Sat Dec 29 2018 08:42:06 GMT+0400 (Gulf Standard Time)

but after receiving data from api JSON result look like this. how to make it same type?

 2018-12-27T13:37:00.83

Typescript

export interface header{
    vr_date : Date  
}

Asp API class

 public class header
 { 
      public Nullable<System.DateTime> vr_date { get; set; }
 }

回答1:


You can use Angular's inbuilt DatePipe. So all you need to do is:

In Component TS:

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

@Component({
 // other stuff
  providers:[DatePipe]
})

constructor(public datePipe : DatePipe){

}

To use it:

var format = "yyyy-MM-dd HH:mm:ss"; // this could be `yyyy-MM-dd` or `MM/dd/yyyy`
this.datePipe.transform(your_date_variable, format);

EDIT:

If you want to convert a date into a GMT then use your_date.toUTCString()

then use:

var isoDate = new Date('2018-12-27T13:37:00.83');
var UTCDate = isoDate.toUTCString();

Which returns:

Thu, 27 Dec 2018 08:07:00 GMT

WORKING DEMO



来源:https://stackoverflow.com/questions/53966873/angular-date-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!