javascript - new Date() - Angular 2

标签 javascript angular

我使用以下代码从输入日期添加一年:

this.maxDate = this.drop.startDate;
this.maxDate.setDate(this.maxDate.getDate() + 365);  

在此上下文中,drop.startDate 是输入变量。但是当我运行此代码时,maxDate 变量将添加 1 年,并且 drop.startDate 值将更改为 maxDate 值。看看图像。有什么指导方针吗?

运行代码之前

Before run the code

运行代码后

After run the code

最佳答案

您必须“克隆”日期对象。否则,startDate 将具有与 maxDate

相同的引用
this.maxDate = new Date(this.drop.startDate.getTime());

关于javascript - new Date() - Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54883032/

相关文章:

javascript - 将查询字符串添加到 data-href

css - Angular Material flex 中心和右对齐

angular - 将超链接添加到 igx-grid 中的列

angular - 如何在我的项目中使用 Chrome 调试 Angular *ngIf 结构指令?

angular - NgRx 效果加载两​​次

javascript - AngularJS单元测试: Initialize scope of a directive's controller

javascript - 递归函数进行搜索

javascript - SAPUI5中的最佳实践表

javascript - 在 switch case 中出现错误时停止循环 - 在将 header 发送到客户端后无法设置 header

Angular FormControl valueChanges 不起作用