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;
}

 

转载于:https://www.cnblogs.com/yu-hailong/p/8087552.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值