c# - 如何在 Windows 10 通用应用程序中将 CalendarDatePicker 中的仅日期值存储到 SQLite 数据库

标签 c# sqlite win-universal-app datetimeoffset windows-10-universal

在我的 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/

相关文章:

c# - C++ 模板和 Java/C# 泛型之间的区别是什么?限制是什么?

c# - 是否可以将 Cortana 用于应用内语音命令?

uwp - AppId 和 Package Id 是否相同?

c# - 基于其他控件的 Gotfocus 显示控件的文本

c# - 为什么 DataContractJsonSerializer 会跳过某些属性的反序列化?

c# - 带有 Mysql ADO.NET 提供程序的 Mysql Ref Cursor

大数据库文件的 SQLite 磁盘 I/O 错误

python - 在插入或忽略后获取主键的 ID

java - 从 SQLite 存储和检索对象

text - UWP 中 'TextBlock' 的绑定(bind)部分