首页 > 解决方案 > 角日期类型

问题描述

从我的角度界面日期值来看是这样的。

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

但是在从 api JSON 结果接收数据后看起来像这样。如何使它成为相同的类型?

 2018-12-27T13:37:00.83

打字稿

export interface header{
    vr_date : Date  
}

ASP API 类

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

标签: c#angulartypescript

解决方案


您可以使用 Angular 的内置DatePipe. 所以你需要做的就是:

在组件 TS 中:

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

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

constructor(public datePipe : DatePipe){

}

要使用它:

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);

编辑:

如果要将日期转换为 GMT,请使用your_date.toUTCString()

然后使用:

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

返回:

Thu, 27 Dec 2018 08:07:00 GMT

WORKING DEMO


推荐阅读