从 Mongodb 格式化 ISODate [英] Formatting ISODate from Mongodb

查看:76
本文介绍了从 Mongodb 格式化 ISODate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Mongodb 中,我以 ISODate 格式存储日期和时间.

In Mongodb I am storing date and time in ISODate format.

看起来像这样

ISODate("2012-07-14T01:00:00+01:00")

使用 nodejs/javascript,我怎样才能显示时间组件,以便我得到这样的东西

Using nodejs/javascript, how can I display the time component so I would get something like this

Time : 01:00

我正在使用 momentjs 来简化此操作,但据我所知,momentjs 似乎支持 ISODate 格式.

I am using momentjs to make this easier but from what I can tell momentjs does seem to support the ISODate format.

感谢您的帮助.

推荐答案

JavaScript 的 Date 对象支持 ISO 日期格式,所以只要你能访问日期字符串,你可以这样做:

JavaScript's Date object supports the ISO date format, so as long as you have access to the date string, you can do something like this:

> foo = new Date("2012-07-14T01:00:00+01:00")
Sat, 14 Jul 2012 00:00:00 GMT
> foo.toTimeString()
'17:00:00 GMT-0700 (MST)'

如果您想要没有秒和时区的时间字符串,那么您可以调用 Date 对象上的 getHours() 和 getMinutes() 方法并自己格式化时间.

If you want the time string without the seconds and the time zone then you can call the getHours() and getMinutes() methods on the Date object and format the time yourself.

这篇关于从 Mongodb 格式化 ISODate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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