0
函數calculateNextDueDate
給出initialDueDate
和interval
返回nextDueDate
。我也需要這個函數來測試傳遞的參數的有效性。有什麼建議麼?先謝謝你。當initialDueDate參數無效時拋出錯誤javascript
const INTERVAL = {
monthly: moment.duration(1, 'months'),
quarterly: moment.duration(3, 'months'),
yearly: moment.duration(1, 'years')
}
function calculateNextDueDate(initialDueDate, intervalCode) {
if (moment().diff(initialDueDate) < 0)
return initialDueDate;
return calculateNextDueDate(initialDueDate.add(INTERVAL[intervalCode]), intervalCode);
}
// usage
console.log(calculateNextDueDate(moment('2016-01-25', 'YYYY-MM-DD'), 'monthly').format('LL'))
console.log(calculateNextDueDate(moment('2016-01-25', 'YYYY-MM-DD'), 'quarterly').format('LL'))
console.log(calculateNextDueDate(moment('2016-01-25', 'YYYY-MM-DD'), 'yearly').format('LL'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
@RobG是這更好的?謝謝:) –