javascriptの練習でカレンダーのプログラムを作っています。setMouthなどを使って次の月の月初と月末を取得しようとしたのですが、setMonthやsetDateの値を、それぞれ変数に入れて保管しようとしたのですが、なぜか最終的に一番新しい値になってしまいます。これは何故なのでしょうか?
javascript
1// 今日の年月日曜日を取得する // 2// 年を取得 3var currentYear = new Date().getFullYear(); // 現在の年を取得 4var currentMonth = new Date().getMonth(); // 現在の月を取得 5var nextMonth = new Date().getMonth() + 1; // 次の月を取得 6var firstDate = new Date(currentYear, currentMonth, 1); // 現在の月初をdate関数にする 7var lastDate = new Date(currentYear, nextMonth, 0); // 現在の月末をdate関するにする 8 9// 来月の年月日曜日を取得する // 10firstDate.setMonth(firstDate.getMonth() + 1); // 次の月初を取得 11var nextFirstDate = firstDate; // firstDateを保管 12firstDate.setMonth(firstDate.getMonth() + 1); // さらに次の月を取得 13firstDate.setDate(firstDate.getDate() - 1); // 月末を取得 14var nextLastDate = firstDate; // firstDateを保管 15console.log(nextFirstDate, nextLastDate); // なぜかここがfirstDataの最新のものになってしまう
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/12 07:02