如何正确保存日期?
我正在尝试保存日期(使用 C# 官方驱动程序):
val = DateTime.Parse(value).Date; //Here date is {11/11/2011 12:00:00 AM}
var update = Update.Set("Date", val);
...
当我从数据库中选择日期时,值为 {11/10/2011 10:00:00 PM}
如何仅保存我想要的日期?
I'm trying to save date (using C# official driver):
val = DateTime.Parse(value).Date; //Here date is {11/11/2011 12:00:00 AM}
var update = Update.Set("Date", val);
...
When I select Date from the database, the value is {11/10/2011 10:00:00 PM}
How to save only the date I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
默认情况下,c# 驱动程序(无需额外设置)将本地日期作为 utc 日期保存到数据库中(日期 - 时区偏移),但不进行任何操作就读回(因此,utc 日期)。
因此,当您从数据库加载日期时间时,您会在 2 小时内收到差异(您的时区偏移量)。有两种方法可以告诉 mongodb c# 驱动程序在反序列化期间将 utc 日期转换为本地时区日期:
1.通过特定日期字段的属性:
2.通过所有日期时间字段的全局设置(默认为
UtcInstance):
一旦您执行#1 或#2,您将看到本地日期。
更新:
#2 在最新的驱动程序版本中已过时,因此请使用下面的代码:
更新:
#2 再次更改:
c# driver by default (without extra settings) saving local dates as utc date into database (date - time zone offset) but reading back without any action (so, utc date).
Because of this when you loading datetime from database you receive diff in 2 hours (your timezone offset). There are two approaches how to say to mongodb c# driver convert utc dates to local timezone dates during deserialization:
1.through the attributes for particular date field:
2.through global settings for all datetime fields (default is
UtcInstance
):Once you will do #1 or #2 you will see local date.
Update:
#2 is obsolete in latest driver version so use code below instead:
Update:
#2 has changed again:
您遇到了时区问题。您的日期对象可能位于 UTC 以外的时区(看起来提前 2 小时),或者您的默认时区设置为 UTC 以外的其他时区。驱动程序会将日期转换为适当的时区,然后再将其存储到数据库中。
通常您不会注意到这一点,因为反向操作(从数据库检索 UTC 日期)应该将其转换回原始时区。您使用的驱动程序可能存在问题,或者 C# 可能需要更多代码才能解决问题。
将日期存储为字符串通常不是一个好主意,因为它会禁用日期的范围查询。
You're running into a timezone issue. Your date object is probably in a timezone other than UTC (2 hours ahead by the looks of it) or your default timezone is set to something other than UTC. The driver will convert the date to the appropriate timezone before storing it in the database.
Normally you wouldn't notice this since the reverse (retrieving the UTC date from the database) should convert it back to the original timezone. There might be an issue with the driver you're using or C# might require a bit more code to get it right.
Storing dates as strings is usually not a good idea since it disables range queries on dates.
Mongo 以 UTC 存储所有内容,如果您的日期时间是 UTC,这会有所帮助
Mongo stores everything in UTC, in case your date time is UTC this will help
2.2.4.26又改变了:
2.2.4.26 has changed again:
就我而言,
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
不起作用。我所做的如下
In my case
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
doesn't worked.What i did is below
Mongodb 日期值以“UTC DateTime”格式存储,其中包含日期和时间。时间。所以你不能在字段中单独存储日期。相反,您可以在从 mongo 检索后在应用程序代码中单独获取日期部分。
就像在 c# 中
或
Mongodb Date value stored in "UTC DateTime" format which comprises of both date & time. so you just cannot store date alone in the field. Instead you can get the date part alone in your application code after retrieving from mongo.
like in c#
or