今天在給mongodb插入日期格式的數據時發現,日期時間相差8個小時,原來存儲在mongodb中的時間是標准時間UTC +0:00,而中國的時區是+8.00 。
因此在插入的時候需要對時間進行處理:
DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
例如:
public void Insert(string spid)
{
Entity1 entity = new Entity1();
entity.SpId = spid;
entity.DT = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
collectionTest.Insert(entity);
}