var date1 = new Date();
date1.setFullYear(2011, 6, 1);
// 2011-07-01, ok
console.log(date1);
// set date2 the same date as date1
var date2 = date1;
// ...
// now I'm gonna set a new date for date2
date2.setFullYear(2011, 9, 8);
// 2011-10-08, ok
console.log(date2);
// 2011-10-08, wrong, expecting 2011-07-01
// I didn't assign a new date to date1
// WHY is date1 changed?
console.log(date1);
最佳答案
日期是对象,因此将其指定为引用 - 简单的方法是
date2 = new Date( date1 );
关于JavaScript 日期变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10767433/