首页 > 解决方案 > 按天数计算的角管返回等级

问题描述

我创建了一个管道来根据过去和当前值之间的天数差异返回类,但它总是返回类,就好像日期总是迟到一样。

我想根据条件关闭的日子返回。

transform(value: any, ...args: any[]): any {
    this.now = Date.now();
    this.now = new Date(this.now).toISOString();
    this.now = Date.parse(this.now);

    this.forecast = new Date(value).toISOString();
    this.forecast = Date.parse(this.forecast);

    this.diff = this.now - this.forecast;

    if(this.forecast < this.now){
      return 'is__border-left-danger';
    }

    else if (this.diff < 172800)  /** less than 2 days */
    {
      return 'is__border-left-danger';
    }

    else if (this.diff > 172800 || this.diff < 345600) /** between 2 days and 4 days */
    {
      return 'is__border-left-warning';
    } 

    else /** otherwise */
    {
      return 'is__border-left-success';
    }
  }

标签: angularionic-framework

解决方案


转换(值:任何,...args:任何 []):任何 { this.now = Date.now();

// Make sure before subtracting dates both should be in milliseconds. If it's not in milliseconds then multiply it by 1000

this.forecast = new Date(value).toISOString();  // I didn't find it usefull
this.forecast = Date.parse(this.forecast);

this.diff = Math.abs(this.now - this.forecast); // Your comparison below need only diff so use Math.abs() function

if(this.forecast < this.now){
  return 'is__border-left-danger';
}

else if (this.diff < 172800)  /** less than 2 days */
{
  return 'is__border-left-danger';
}

else if (this.diff > 172800 || this.diff < 345600) /** between 2 days and 4 days */
{
  return 'is__border-left-warning';
} 

else /** otherwise */
{
  return 'is__border-left-success';
}

}

检查这个可能有用=> https://www.epochconverter.com/


推荐阅读