如何在Angular2中为日期添加天数? [英] How to add days to a date in Angular2?

查看:44
本文介绍了如何在Angular2中为日期添加天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想创建一个从现在起 3 天后用于打字稿角度组件的日期.

I simply want to create a date that is 3 days from now for use in a typescript angular component.

我看过Angular2 moment 但这似乎只与管道有关.虽然我确实看到了这篇文章 关于在代码中使用管道,看起来有点 hacky...

I have looked at Angular2 moment but this seems only to relate to pipes. Although I did see this article about using pipes in code, looks a bit hacky...

也许我遗漏了一些东西,因为这应该很简单?!

Maybe I'm missing something as this should be really simple?!

谢谢

推荐答案

date: Date;

ngOnInit() {
  this.date = new Date();
  this.date.setDate( this.date.getDate() + 3 );
}

然后你可以使用日期管道在你的 HTML 中很好地显示日期

Then you can use the date pipe to display the date nicely in your HTML

{{ date | date }}

如此呈现:

2017 年 9 月 25 日

Sep 25, 2017

这篇关于如何在Angular2中为日期添加天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆