【问题标题】:Angular Date type角日期类型
【发布时间】:2018-12-29 05:09:58
【问题描述】:

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

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

【问题讨论】:

  • 数据库中的数据类型是什么?是日期时间吗?
  • 数据库字段为日期时间
  • 好的,参考发布的答案!
  • 不工作相同的结果。 this.header.vr_date.getFullYear().toString().substring(2,4) 给出错误
  • 那是在 C# 中吗..?

标签: c# angular typescript


【解决方案1】:

您可以使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多