c# - 如何将长毫秒(纳秒)部分的字符串解析为 DateTime?

标签 c# string datetime

我正在尝试找出以下给定日期时间文字的正确字符串格式:

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/

相关文章:

c# - 使用 SFTP 从 FTP 站点上传下载文件

javascript - Web api 的 POST 参数设置为 null

Java:替换字符串中的字符

PHP/MySQL 日期/时间差异

c# - 垃圾回收失败回收BitmapImage?

c# - ObservableCollection.Remove - 无法将 lambda 表达式转换为类型,因为它不是委托(delegate)类型

java - 使用正则表达式分割字符串,但在子字符串中包含部分正则表达式

python - 通过替换现有文本中的单词进行翻译

python - 日期时间列python的百分位数

python - 将日期(系列)列从一个 DataFrame 添加到另一个 Pandas,Python