首页 > 解决方案 > 如何避免在这个 js 日期总和中重置原始变量

问题描述

我有这个简单的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 death date
    console.log(death_date) //logs death date

如何避免在设置死亡日期后将出生日期设置为死亡日期?

标签: javascript

解决方案


var birth = new Date();
var death = new Date(birth.getTime())

death.setFullYear(birth.getFullYear() + 100);

console.log('Birth: ' + birth);
console.log('Death: ' + death);

推荐阅读