角度日期类型 [英] Angular Date type
本文介绍了角度日期类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从我的角度界面日期值是这样的.
From my angular interface date value coming like this.
Sat Dec 29 2018 08:42:06 GMT+0400 (Gulf Standard Time)
但是在从 api JSON 结果接收数据后看起来像这样.如何使其类型相同?
but after receiving data from api JSON result look like this. how to make it same type?
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; }
}
推荐答案
你可以使用 Angular 的内置 DatePipe
.所以你需要做的就是:
You can use Angular's inbuilt DatePipe
. So all you need to do is:
在组件 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()
If you want to convert a date into a GMT then use 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
这篇关于角度日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文