私はプロジェクトでmomentJS
を使用していますが、私はmonth
とyear
を受け取り、それらのパラメータを使用して月の最終日を返します。Moment JS月末の問題
すべてはうまく機能しています.1月から11月にかけて、12月を使用するとすぐに1月を返します。
どのように私はこれを動作させるために微調整できますか?真の月の値(5月= 5月)を渡してから、関数内で月を減算して、正しく機能するように瞬時に0にします。
フィドル:https://jsfiddle.net/bhhcp4cb/
// Given a year and month, return the last day of that month
function getMonthDateRange(year, month) {
// month in moment is 0 based, so 9 is actually october, subtract 1 to compensate
// array is 'year', 'month', 'day', etc
var startDate = moment([year, month]).add(-1,"month");
// Clone the value before .endOf()
var endDate = moment(startDate).endOf('month');
// make sure to call toDate() for plain JavaScript date type
return { start: startDate, end: endDate };
}
// Should be December 2016
console.log(moment(getMonthDateRange(2016, 12).end).toDate())
// Works fine with November
console.log(moment(getMonthDateRange(2016, 11).end).toDate())
より読みやすいと思う、そのいずれかをキャッチしていない - あなたに感謝! – SBB