Javascript 如何避免在这个js日期和中重置原始变量
我有一个简单的javascript函数来计算日期Javascript 如何避免在这个js日期和中重置原始变量,javascript,Javascript,我有一个简单的javascript函数来计算日期 console.log(this.state.birth_date) //logs birth date var death_date = this.state.birth_date.setFullYear(this.state.birth_date.getFullYear() + this.state.years_to_live) console.log(this.state.birth_date) //logs
console.log(this.state.birth_date) //logs birth date
var death_date = this.state.birth_date.setFullYear(this.state.birth_date.getFullYear() + this.state.years_to_live)
console.log(this.state.birth_date) //logs death date
console.log(death_date) //logs death date
如何避免在设定死亡日期后将出生日期设定为死亡日期?这是否回答了您的问题?
var birth = new Date();
var death = new Date(birth.getTime())
death.setFullYear(birth.getFullYear() + 100);
console.log('Birth: ' + birth);
console.log('Death: ' + death);