MongoDB insertion Time

Source: Internet
Author: User
Tags getdate mongodb

Insert Time:

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

When data was inserted into the date format for MongoDB, it was found that the date time was 8 hours apart, the time originally stored in MongoDB was the standard time UTC +0:00, while China's time zone was +8.00.

correct when removed

Db.test.find () [0].time.gethours ()

It is therefore necessary to process the time when inserting:

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")})

With custom functions:

functionInsertdate (Time) {time.sethours (time.gethours ()-8); returnTime ;} Insertdate (NewDate (2017,9,9,9,63,72))functionGetformatdate (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; returnNewTime;}

MongoDB insertion Time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.