在MongoDB中将字符串转换为日期 [英] Converting string to date in mongodb

查看:639
本文介绍了在MongoDB中将字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以使用mongodb shell使用自定义格式将字符串转换为日期

Is there a way to convert string to date using custom format using mongodb shell

我正在尝试将"21/May/2012:16:35:33 -0400"转换为日期,

I am trying to convert "21/May/2012:16:35:33 -0400" to date,

是否有办法将DateFormatter或其他东西传递给 Date.parse(...)ISODate(....)方法?

Is there a way to pass DateFormatter or something to Date.parse(...) or ISODate(....) method?

推荐答案

您可以在Ravi Khakhkhar提供的第二个链接中使用javascript,否则您将不得不执行一些字符串操作来转换原始字符串(例如原始格式的特殊字符不会被识别为有效的分隔符),但一旦这样做,您就可以使用新"

You can use the javascript in the second link provided by Ravi Khakhkhar or you are going to have to perform some string manipulation to convert your orginal string (as some of the special characters in your original format aren't being recognised as valid delimeters) but once you do that, you can use "new"

training:PRIMARY> Date()
Fri Jun 08 2012 13:53:03 GMT+0100 (IST)
training:PRIMARY> new Date()
ISODate("2012-06-08T12:53:06.831Z")

training:PRIMARY> var start = new Date("21/May/2012:16:35:33 -0400")        => doesn't work
training:PRIMARY> start
ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")

training:PRIMARY> var start = new Date("21 May 2012:16:35:33 -0400")        => doesn't work    
training:PRIMARY> start
ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")

training:PRIMARY> var start = new Date("21 May 2012 16:35:33 -0400")        => works
training:PRIMARY> start
ISODate("2012-05-21T20:35:33Z")

有些链接可能对您有用(关于mongo shell中数据的修改)-

Here's some links that you may find useful (regarding modification of the data within the mongo shell) -

http://cookbook.mongodb.org/patterns/date_range/

http://www.mongodb.org/display/DOCS/Dates

http://www.mongodb.org/display /DOCS/Overview +-+++ MongoDB + Interactive + Shell

这篇关于在MongoDB中将字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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