javascript - 如何避免在这个 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
如何避免在设置死亡日期后将出生日期设置为死亡日期?
解决方案
var birth = new Date();
var death = new Date(birth.getTime())
death.setFullYear(birth.getFullYear() + 100);
console.log('Birth: ' + birth);
console.log('Death: ' + death);
推荐阅读
- android - android,无法更改为 com.android.tools.build:gradle:4.2.1
- javascript - 当显示特定文本时,我需要隐藏一个元素
- android - 触摸/单击 ListView 中的项目时,有时会丢失按下状态
- python - 如何使用 selenium python 在 Edge 中将网页语言从英语更改为其他语言?
- elasticsearch - 在 Elasticsearch 中搜索可搜索字段
- bash - 如何在 Bash 中执行由数组表示的命令
- powerbi - 具有 2 个不同数据对象的 Vega-lite 堆叠条形图
- c++ - 为什么 std::align 将对指针的引用作为参数
- python - 在给定字符串中查找字符串的位置
- sql-server - Azure 数据工厂管道混淆了数据库访问帐户