我有moment.js对象处于状态
state = {
startDate: getNow() //this funtion return a momemtJS object
}
Run Code Online (Sandbox Code Playgroud)
在一个函数中,我需要获取startDate前一年的日期
const dateBeforeOneYear = this.state.startDate.subtract(1,'years');
但是,如果我这样做,则会错误地修改状态
所以我尝试复制状态
const copyStartDate = {...this.state.startDate}
const copyStartDate = this.state.startDate.subtract(1, 'years');
Run Code Online (Sandbox Code Playgroud)
但是现在我得到了错误,减号不是函数,我想是因为copyStartDate不再是MomemntJs
有一种克隆时刻对象的方法:
const yearBefore = this.state.startDate.clone().subtract(1, 'years');
Run Code Online (Sandbox Code Playgroud)
最好将一个可序列化的日期表示形式存储在组件状态中,例如,调用.valueOf()Date或Moment的结果,二者之一都返回自UNIX时代以来的毫秒数。
| 归档时间: |
|
| 查看次数: |
1357 次 |
| 最近记录: |