在我的 Windows 10 通用应用程序中,我使用 CalendarDatePicker 选择日期:
<CalendarDatePicker x:Name="cdpStartDate" Width="150" />
在代码后面,我使用以下代码将日期存储到 SQLite 数据库:
booking = new Bookings();
using (
SQLite.Net.SQLiteConnection conn =
new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), Common.Utils.DbPath()))
{
booking.StartDate = cdpStartDate.Date.ToDateTime();
conn.Update(booking);
}
我的表类如下所示:
[Table("Bookings")]
public class Bookings
{
[PrimaryKey]
public int BookingId { get; set; }
[NotNull]
public DateTime StartDate { get; set; }
}
调试 booking.StartDate 的值为 18.11.2015 00:00:00。但将该值保存到 SQLite 数据库后,该值将转换为 17.11.2015 23:00:00。这似乎是 UTC 时间。 我的应用程序中不需要任何时间信息,只需要日期。
如何在不转换到数据库的情况下保存日期?
谢谢, 乌韦
最佳答案
时间戳通常只是某一天的 0:00 时间。由于 SQLite 不为您处理时区,因此如果您想获得纯日期,则必须输入纯(无时区)日期时间。
尝试:
booking.StartDate = cdpStartDate.Date.ToDateTime().ToUniversalTime().Date;
它实际上是 DateTime 类,非常友好可以向其中添加您的本地时区信息。
关于c# - 如何在 Windows 10 通用应用程序中将 CalendarDatePicker 中的仅日期值存储到 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157233/