从Oracle转换为datetime
本文关键字:datetime 转换 Oracle | 更新日期: 2023-09-27 18:11:53
我知道有很多类似的问题,但是我找不到我要找的。
这是我的oracle日期:
string testdate= "2014-01-07 15:00:00.0000000";
下面是我如何转换为datetime:
DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)
这会抛出一个格式异常。什么好主意吗?
我的快速测试还抛出字符串无效日期时间异常。快速测试:
Console.WriteLine(DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture).ToShortDateString());
我将从一开始就尽量避免将作为字符串使用。请确保您在Oracle中使用了适当的数据类型,并且您应该能够在适当的DataReader
(或任何您正在使用的)上调用GetDateTime
。
如果必须将其解析为文本,那么您需要指定与值匹配的格式-因此使用7 f
s而不是3,因为您的值具有"。0000000"
DateTime.ParseExact(testdate, "yyyy-MM-dd HH:mm:ss.fffffff",
CultureInfo.InvariantCulture)
但是,我再次强烈建议避免将值作为文本处理。
为什么要使用ParseExact
?常规的Parse
似乎可以工作。
var dt = DateTime.Parse("2014-01-07 15:00:00.0000000", CultureInfo.InvariantCulture);
// Prints out 2014-01-07T15:00:00.0000000
Console.WriteLine(dt.ToString("o"));