mongodb插入时间


插入时间:

db.test.insert({time:new Date()})

给mongodb插入日期格式的数据时发现,日期时间相差8个小时,原来存储在mongodb中的时间是标准时间UTC +0:00,而中国的时区是+8.00 。

取出时正确

db.test.find()[0].time.getHours()

因此在插入的时候需要对时间进行处理:

db.test.insert({"Time":new Date(new Date().getFullYear()+"-"+(new Date().getMonth()+1)+"-"+new Date().getDate()+ " "+new Date().toLocaleTimeString())})

db.test.insert({'time':ISODate("2012-11-02 07:58:51")})

db.user.uinfo.insert({'ltime':ISODate("2012-11-02 07:58:51")})

用自定义函数:

function insertDate(time){
    time.setHours(time.getHours()-8);
    return time;
}
insertDate(new Date(2017,9,9,9,63,72))

function getFormatDate(time){
    year = time.getFullYear();
    mon = time.getMonth()+1;
    date = time.getDate();
    hour = time.getHours();
    min = time.getMinutes();
    sec = time.getSeconds();
    newtime = year+'-'+mon+'-'+date+' '+hour+':'+min+':'+sec;
    return newtime;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM