Angular Typescript如何将数学公式与日期对象子字符串一起使用



我正在尝试转换以下公式,该公式在Typescript中的excel中有效。有人能帮我怎么做到这一点吗。我正在获取'The left -hand and right hand side of an arithmetic operation must be of type 'any', 'number' or an enum type'

const result = ((inceptionDate) - 365.25 - referenceInceptionDate) / 365.25 + (riskFactor))

仅供参考:inceptionDate和referenceInceptionDate都是Date对象。

尝试了这个,没有得到任何错误。这是正确的路吗?

const result: number = ((inceptionDate.valueOf() - 365.25 - entry.referenceInceptionDate.valueOf()) / 365.25 + (riskFactor));

感谢

您正在尝试对日期对象执行计算。这不是一个本机数值对象。您需要重新编写代码才能使用类似valueOf((方法的东西。

或者,如果你有很多这样类型的计算,那么可以考虑Moment.js.等日期库

最新更新