How to convert ISODate to DateTime?

天大地大妈咪最大 提交于 2019-12-23 04:08:36

问题


In a collection I have IsoDates stored as follows:

 SubmitDateTime" : ISODate("2015-03-02T07:39:05.463Z")

Now I want to map this property to MyModel with the following property:

public class MyModel
{
    public DateTime SubmitDateTime { get; set; }
}

And then map it as follows:

GetCollection<MyModel>("collection_name").Find(myQuery).ToListAsync();

Unfortunately I get this error:

System.ArgumentOutOfRangeException
The value -9223372036854775808 for the BsonDateTime MillisecondsSinceEpoch is outside therange that can be converted to a .NET DateTime

Does anyone know how to fix this?

Thanks.

PS I originally converted string representations of this format "2015-03-30T10:50:01.813" to the IsoDate with this script in the Mongo shell:

var cursor = db.collection_name.find()
while (cursor.hasNext()) {
  var doc = cursor.next();
  db.collection_name.update({_id : doc._id}, {$set : {SubmitDateTime : new Date(doc.SubmitDateTime)}})
}

Perhaps an improvement can be made there.

来源:https://stackoverflow.com/questions/31627240/how-to-convert-isodate-to-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!