A difference of eight hours a solution when c # mongodb save time type field

        /// <summary>
        /// 添加时间
        /// </summary>
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime CreatedTime { get; set; }

This feature can be added above. Adding this feature is not to say mongodb database when saving time is local time. But that mongodb drive will be automatically converted into local time according to this feature when displayed. Actually stored in the database or the time zone of 0:00.

Why he would say, because the read java mongodb driver source code, know that java is doing, then it should also .net. Posted below java mongodb driver source code:

        if (o instanceof Date) {
            Date d = (Date) o;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
            serialize(new BasicDBObject("$date", format.format(d)), buf);
            return;
        }

From source to see if he met the type of time, no matter how much the value of your time will be regenerated in accordance with 0:00 time zone. We can only show when converted into local time.

Guess you like

Origin www.cnblogs.com/subendong/p/11910731.html