获取多条列表之间的月数
const billList = [{itemId:'3cyuu8',billStartDate:'2018-09-12',billEndDate:'2018-09-22'},{itemId:'3cyuu8',billStartDate:'2018-09-15',billEndDate:'2018-09-30'},{itemId:'3cyuu8',billStartDate:'2018-10-01',billEndDate:'2018-10-30'}]
const moment = require('moment')
function getMonthCountByItemId (itemId) {
let useBillList = billList.filter(item => item.itemId === itemId)
return useBillList.reduce((monthCount, item) => {
let start = moment(item.billStartDate)
let end = moment(item.billEndDate)
console.log(item.billStartDate, item.billEndDate)
do {
console.log(start.format('YYYY-MM-DD'))
let inAMonth = (start.year() === end.year() && start.month() === end.month())
if (inAMonth) {
console.log('count', (end.diff(start, 'day') + 1) / start.daysInMonth())
monthCount += (end.diff(start, 'day') + 1) / start.daysInMonth()
break
} else {
let daysToMonthEnd = start.daysInMonth() - start.date() + 1
console.log('count', daysToMonthEnd / start.daysInMonth())
monthCount += daysToMonthEnd / start.daysInMonth()
start.add(daysToMonthEnd, 'day')
}
} while (start.isBefore(end))
return monthCount
}, 0)
}
console.log(getMonthCountByItemId('ec48ab69-b5f0-4066-b855-4608638cab2a'))