【问题标题】:Display Today, yesterday, tommorrow instead of dates in ionic显示今天,昨天,明天而不是离子中的日期
【发布时间】:2018-07-20 07:55:46
【问题描述】:

我正在制作一个 ionic 应用程序 v2,其中我必须显示“今天”而不是当前日期,以及“昨天”和“明天”也适用于各自的日期。我尝试使用时刻,但它给出了像上周一一样到上周的天数,并且参考时间问题也存在于时刻。我只需要这 3 天没有参考时间。你能告诉我如何在离子框架中自定义时刻吗?如果您有任何其他建议,而不是使用 moment 。请告诉。提前致谢。 P.S:我只想在 ionic 的 html 代码中实现这一点,而不是在打字稿代码中。

【问题讨论】:

  • 你能给我们一些代码库吗?我们对您的项目一无所知。注意:没有js/ts会很困难。
  • 我正在使用 ts 和 html 开发 ionic 框架

标签: ionic-framework ionic2 momentjs angular2-moment


【解决方案1】:

就像这样:moment().add(-1, 'days')。它会为您提供前一天与您本地电脑上相同的当前时间。

参考here

【讨论】:

  • 我没明白应该在哪里添加这个 moment().add() 函数。是在ts文件里吗?
【解决方案2】:

同意如果没有 JS/TS,这将很困难。在你的 .ts 文件中你不能有 3 个日期成员变量:

//Set up 3 new dates, defaulting them to today
yesterday: Date = new Date();
today: Date = new Date();
tomorrow: Date = new Date();

然后在您的 ctor 或 init 方法中,正确设置它们(下面可能不是最有效/最有效的方法,但只是一个示例)。

//Today is already set up from instantiation, but re-set tomorrow and yesterday    
this.tomorrow.setDate(this.tomorrow.getDate() + 1);
this.yesterday.setDate(this.yesterday.getDate() - 1);

然后在您的 HTML 中,绑定到它们:

Yesterday was: {{yesterday?.toDateString()?.slice(0,10)}}
<br> Today is: {{today?.toDateString()?.slice(0,10)}}
<br> Tomorrow will be: {{tomorrow?.toDateString()?.slice(0,10)}}

【讨论】:

  • 感谢您的回答,但它在做什么..它显示日期。我不需要那个。我已经有约会了。我想显示今天,昨天而不是那天的日期。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多