initialDueDate
とと与えられた関数calculateNextDueDate
はnextDueDate
を返します。また、渡される引数の妥当性をテストできるようにするには、この関数が必要です。助言がありますか?前もって感謝します。 ?あなたはinitialDueDateは(日付文字列が何であるかを言うことはありませんinitialDueDateパラメータが無効なときにエラーをスローするjavascript
if (!moment(initialDueDate).isValid()) {
throw new Error('Invalid date: ' + initialDueDate);
}
:
validationためMoment.jsのドキュメントによると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はこの良いです?ありがとう:) –