function calcYearAndDateDiff(startDate, endDate) {
const startDateClone = startDate.clone()
const diffYear = endDate.diff(startDateClone, 'year')
const diffDay = endDate.diff(startDateClone.add(diffYear,'years'), 'day')
return [diffYear, diffDay]
}
const startDate= moment('2000-01-01')
const today = moment()
const tomorrow = moment().add(1,'days')
const diff1 = calcYearAndDateDiff(startDate, today)
const diff2 = calcYearAndDateDiff(startDate, tomorrow)
我需要得到js实现两个数字相加的结果,可是js总是默认按照字符串处理,将两个数字连接起来。解决这个问题,代码如下:
<script type="text/javascript">var a = 1var b = 2var sum = parseInt(a) + parseInt(b)</script>
1
2
3
4
5
parseInt()是js的解析字符串的函数,返回解析后的数字。