我正在尝试找出以下给定日期时间文字的正确字符串格式:
18-JUN-13 12.17.36.000000000
使用 MSDN ,我设法组成了以下格式:
"d'-'MMM'-'y'T'h'.'m'.'s'.'fff"
但是当使用 DateTime.ParseExact
时,解析失败并出现 FormatException
:String 未被识别为有效的 DateTime。 p>
我的代码:
DateTime.ParseExact("18-JUN-13 12.17.36.000000000", "d'-'MMM'-'y'T'h'.'m'.'s'.'fff", null);
最佳答案
你可以使用
dd-MMM-yy hh.mm.ss.fffffff
具有基于英语 的文化,例如 InvariantCulture
例如。我现在在手机上,所以我不能试试:(
AFAIK,毫秒部分解析限制为 7
,这就是为什么您不操作字符串就无法解析它的原因。你可以像这样使用它;
var dt = DateTime.ParseExact("18-JUN-13 12.17.36.0000000",
"dd-MMM-yy HH.mm.ss.fffffff",
CultureInfo.InvariantCulture);
看起来这就是为什么我们可能有 The "fffffff"
custom format作为毫秒的顶级字符。来自文档;
Although it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
你问了;
How would you have manipulate the string?
好吧,一种获取逗号的最后一个索引并将其子串到 8 索引之后的方法;
string s = "18-JUN-13 12.17.36.000000000";
var dateString = s.Substring(0, s.LastIndexOf(".") + 8);
关于c# - 如何将长毫秒(纳秒)部分的字符串解析为 DateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28409624/