在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5/ Angular4.0 format() 方法報錯

Angular4.0 format() 方法報錯

一個日期轉(zhuǎn)換方法,鼠標放在format上 與 放在 subDays 上都有顯示
總是提示多一個參數(shù),哪里出問題了?

export interface Age {
  age: number;
  unit: AgeUnit;
}
    const age$ = Observable
      .combineLatest(ageNum$, ageUnit$, (_n, _u) => {
        return this.toDate({age: _n, unit: _u});
    })

圖片描述

  toDate(age: Age): string {
    const now = Date.now();
    const dateFormat = 'YYYY-MM-DD';
    switch (age.unit) {
      case AgeUnit.Year: {
        return format(subYears(now, age.age), dateFormat);
      }
      case AgeUnit.Month: {
        return format(subMonths(now, age.age), dateFormat);
      }
      case AgeUnit.Day: {
        return format(subDays(now, age.age), dateFormat);
      }
      default: {
        return null;
      }
    }
  }

圖片描述
鼠標放在format上,錯誤提示如下:
圖片描述

鼠標放在subDays上,錯誤提示如下:
圖片描述

回答
編輯回答
巴扎嘿

你沒有給出format 函數(shù)的原型,所以只能靠猜了,有一個時間格式化的函數(shù)庫叫format.js
提供了類似的方法,但是它把format 定義在原型鏈上的,所以調(diào)用的時候 是類似這樣date.format('mm-dd-YYYY')

又不知道你的subxxx 系列函數(shù)返回的值是什么,如果返回的直接是個Date Object
哪你的調(diào)用方式 應(yīng)該是

 return subYears(now, age.age).format(dateFormat);
//如果不是,哪還要先轉(zhuǎn)換為Date對象
2017年6月4日 09:12
編輯回答
影魅

看截圖應(yīng)該是format多傳了一個參數(shù),查了一下Angular文檔,應(yīng)該不是Angular本身的方法。

如果你想要日期轉(zhuǎn)換的效果,你可以考慮 pipe

或者試一下這個formatDate

2017年2月8日 18:24