使用角度4显示年份

时间:2018-01-30 11:41:18

标签: angular

我正在尝试使用角度4在屏幕上显示两年的值。第一个是当前年份,第二个是当前年份加上7.如果我以正确的方式进行操作,请告诉我。

组件代码

  currentYear = new Date();
  currentYearPlusSix = new Date();
  currentYearPlusSix1 = this.currentYearPlusSix.setDate(this.currentYearPlusSix.getFullYear() + 7 );

HTML

 The current year is {{currentYear | date:'yyyy'}}
  The current year plus six is {{currentYearPlusSix1 | date:'yyyy'}}

3 个答案:

答案 0 :(得分:1)

使用setFullYear设置年份,而不是setDate

setDate显然是错误的用法,会产生错误的结果。

 currentYearPlusSix1 = this.currentYearPlusSix.setFullYear(this.currentYearPlusSix.getFullYear() + 7 );

setDate接受dayValue - 表示该月中某天的整数。如果你过了一年,你会得到不同的结果



currentYear = new Date();
currentYearPlusSix = new Date();
currentYearPlusSix1 = this.currentYearPlusSix.setDate(this.currentYearPlusSix.getFullYear() + 7);

console.log(currentYearPlusSix)




使用setFullYear代替更改年份



currentYear = new Date();
currentYearPlusSix = new Date();
currentYearPlusSix1 = this.currentYearPlusSix.setFullYear(this.currentYearPlusSix.getFullYear() + 7);

console.log(currentYearPlusSix)




答案 1 :(得分:0)

请进行以下更改:

currentYearPlusSix1 = this.currentYearPlusSix.setFullYear(this.currentYearPlusSix.getFullYear() + 7 );

setDate只会添加天数。

答案 2 :(得分:0)

您需要使用currentYearPlusSix1 = this.currentYearPlusSix.setFullYear(this.currentYearPlusSix.getFullYear() + 7 );

  mainFunction(): Promise<boolean> {

     return this.function1().then(
      (data) => {

        if(data.condition1()){
          this.function2()
            .then(() => {
              return true // here my problem
            })
        } else if (data.condition2()){
          return true;
        } else {
          return false;
        }
      }
    );
  }

  function1: Promise<string>{
    //return promise<string>
  } 

  function2: Promise<any>{
    //return promise<any>
  }

您可以参考this了解更多信息。