【问题标题】:How to avoid the original variable being reset in this js date sum [duplicate]如何避免在这个js日期总和中重置原始变量[重复]
【发布时间】:2020-01-10 15:23:28
【问题描述】:

我有这个简单的 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


【解决方案1】:
var birth = new Date();
var death = new Date(birth.getTime())

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

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

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 2019-11-28
    • 2019-06-22
    • 1970-01-01
    • 2010-09-13
    • 2023-03-06
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多