c# - 序列化:来自数据库的 DateTime.Now VS DateTime
问题描述
上下文:我正在序列化一些数据,其中包括我需要显示毫秒的日期。我正在使用 XmlSerializer 来编写 XML 文件。我正在使用 DataTable 从数据库中获取一些数据。
我实际上在做什么:我以这种方式投下日期
CDate(dr("MyDate")).ToUniversalTime()
但是一旦序列化,这不包括毫秒。
在尝试一些解决方法时,我发现以相同的方式进行序列化
DateTime.Now.ToUniversalTime()
确实包括毫秒。
实际问题:在序列化 dr("MyDate") 时我应该怎么做才能包含毫秒?
请注意,VB.NET 或 C# 中的答案都被广泛接受。
解决方案
Have you checked, what CDate contains? For exmaple Definition of CDate shows that CDate contains only seconds, not milliseconds. When the original time in the database is stored in text format (with milliseconds) you should use DateTime.TryParseExact(...) to get the complete time value. Otherwise, when the database itself uses another DateTime-class, you should use this type directly to convert to datetime.
推荐阅读
- sql - 如何获取xml节点的数量
- spring-boot - 在 AWS (EKS) 上运行 MySQL 集群支持、支持 HTTPS 的 Spring Boot 应用程序
- react-native - Redux mapStateToProps 只调用一次
- ios - Swift 单元测试:检查同一类的两个对象是否相等
- python-3.x - 将 csv 文件读取到 jupyter 时忽略索引
- c# - 在 asp.net 核心中使用 VerifyHashedPassword 时出现 NullReferenceException
- java - Ant 构建和 Eclipse 构建不同的结果
- c++ - Windows/C++ -- 将图标渲染为具有透明度的位图
- google-apps-script - 使用 Selection 的 RangeElements 获取 Google Doc 中的所有嵌套文本元素
- javascript - 将上下文信息绑定到 GraphQL 类型?