Javascript Moment.js将UTC从字符串转换为Date对象 [英] Javascript momentjs convert UTC from string to Date Object

查看:157
本文介绍了Javascript Moment.js将UTC从字符串转换为Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们, 在moment.js文档上遇到困难.

Folks, Having a difficult time with moment.js documentation.

record.lastModified = moment.utc().format();

返回:

2014-11-11T21:29:05+00:00

哪个很棒,它在UTC中...当我将它存储在Mongo中时,它存储为String,而不是我想要的Date对象类型.

Which is Great, its in UTC... When I store that in Mongo, it gets stored as a String, not a Date object type, which is what i want.

我需要的是:

"lastModified" : ISODate("2014-11-11T15:26:42.965-0500")

但是我需要它是 native javascript对象类型,并将其存储在Mongo中.现在,如果我存储以上内容,它将以字符串形式而不是Date对象类型进入.

But I need it to be a native javascript object type, and store that in Mongo. Right now if i store the above, it goes in as string, not Date object type.

我已经尝试了使用moment.js进行的几乎所有操作.他们的toDate()函数可以工作,但会回退到我的本地时区,并且没有给我utc.

I have tried almost everything with moment.js. Their toDate() function works, but falls back to my local timezone, and not giving me utc.

谢谢!

推荐答案

保存Javascript Date对象将导致ISODate存储在Mongo中.

Saving a Javascript Date object will result in an ISODate being stored in Mongo.

将ISO日期保存为Javascript String将导致String存储在Mongo中.

Saving an ISO date as a Javascript String will result in a String being stored in Mongo.

所以,这就是您想要的:record.lastModified = new Date(moment().format());

So, this is what you want: record.lastModified = new Date(moment().format());

这篇关于Javascript Moment.js将UTC从字符串转换为Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆