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