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

查看:391
本文介绍了从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天全站免登陆