最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】js中如何把moment转换为Date对象

JS crifan 9305浏览 0评论

折腾:

【已解决】Preact中实现日期时间选择

期间,需要在js中把moment转换为Date对象,以避免两个react中的state中的变量,指向同一个moment对象,导致两个值始终是一样了。

js moment to date

javascript – Moment.js transform to date object – Stack Overflow

【未解决】js中如何查看到当前对象变量的类名

还是暂时先用:

instanceof – JavaScript | MDN

去看看是否是某个类的实例

代码:

<code>console.log(`before select: this.state.selected_time=${this.state.selected_time}`);
// console.log(`curMoment type=${typeof this.state.curMoment}`);
// console.log(`selected_time type=${typeof this.state.selected_time}`);
// console.log(`curMoment type name=${this.state.curMoment.constructor.getName()}`);
// console.log(`selected_time type name=${this.state.selected_time.constructor.name}`);
console.log(`curMoment instanceof moment=${this.state.curMoment instanceof moment}`);
console.log(`selected_time instanceof Date=${this.state.selected_time instanceof Date}`);
console.log(`selected_time instanceof moment=${this.state.selected_time instanceof moment}`);

this.setState({
isSelectTimeVisible: false,
selected_time: this.state.curMoment
});

console.log(`after select: this.state.selected_time=${this.state.selected_time}`);
// console.log("curMoment type=", typeof this.state.curMoment);
// console.log("selected_time type=", typeof this.state.selected_time);
console.log(`curMoment instanceof moment=${this.state.curMoment instanceof moment}`);
console.log(`selected_time instanceof Date=${this.state.selected_time instanceof Date}`);
console.log(`selected_time instanceof moment=${this.state.selected_time instanceof moment}`);
</code>

结果:

<code>before select: this.state.selected_time=null
index.js?7207:133 curMoment instanceof moment=true
index.js?7207:134 selected_time instanceof Date=false
index.js?7207:135 selected_time instanceof moment=false
index.js?7207:142 after select: this.state.selected_time=1521859844181
index.js?7207:145 curMoment instanceof moment=true
index.js?7207:146 selected_time instanceof Date=false
index.js?7207:147 selected_time instanceof moment=true
</code>

 

果然是出问题了:

在选择时间后,selected_time从Date变成moment了。

-》而不是我们希望的,保持Date。

所以还是要继续去转换

‎momentjs.cn

moment.toDate() returns some internal object when using the array constructor · Issue #624 · moment/moment

Moment.js | Docs

然后代码:

<code>    console.log(`before select: this.state.selected_time=${this.state.selected_time}`);
    console.log(`curMoment instanceof moment=${this.state.curMoment instanceof moment}`);
    console.log(`selected_time instanceof Date=${this.state.selected_time instanceof Date}`);
    console.log(`selected_time instanceof moment=${this.state.selected_time instanceof moment}`);

    let convertedDate = this.state.curMoment.toDate();
    console.log(`convertedDate instanceof Date=${convertedDate instanceof Date}`);

    this.setState({
      isSelectTimeVisible: false,
      selected_time: convertedDate
    });

    console.log(`after select: this.state.selected_time=${this.state.selected_time}`);
    console.log(`curMoment instanceof moment=${this.state.curMoment instanceof moment}`);
    console.log(`selected_time instanceof Date=${this.state.selected_time instanceof Date}`);
    console.log(`selected_time instanceof moment=${this.state.selected_time instanceof moment}`);
</code>

效果是:

<code>before select: this.state.selected_time=Sat Mar 24 2018 10:55:56 GMT+0800 (CST)
index.js?7207:129 curMoment instanceof moment=true
index.js?7207:130 selected_time instanceof Date=true
index.js?7207:131 selected_time instanceof moment=false
index.js?7207:134 convertedDate instanceof Date=true
index.js?7207:141 after select: this.state.selected_time=Wed Mar 14 2018 10:55:56 GMT+0800 (CST)
index.js?7207:142 curMoment instanceof moment=true
index.js?7207:143 selected_time instanceof Date=true
index.js?7207:144 selected_time instanceof moment=false
</code>

得到我们要的了:

把moment转换为Date

log打印出就是:Wed Mar 14 2018 10:55:56 GMT+0800 (CST)

【总结】

moment转换为Date,直接用内置支持的:

<code>convertedDate = someMomentInstance.toDate();
</code>

即可。

转载请注明:在路上 » 【已解决】js中如何把moment转换为Date对象

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.181 seconds, using 22.00MB memory