c# - 如何解析日期时间秒.ms

标签 c# parsing datetime datetimeoffset

我有一个字符串

1368352924.281610000

呈现日期时间

我该如何解析这个?

已尝试:

string rststr = Convert.ToString(result);                       
string[] rststrarr = rststr.Split('.');
DateTime.Parse(rststrarr[0]);

编辑 抱歉造成困惑。它指出这是一个高分辨率的unix时间戳。 亲切的问候。

最佳答案

这个数字很可能代表自 UTC 1970 年 1 月 1 日以来经过的秒数。这是“Unix Epoch”值,在许多系统和日期格式中都很常见。

假设我对您所拥有的值类型的判断是正确的,那么您可以执行以下操作:

// you said you were starting with a string
string s = "1368352924.281610000";

// but you are going to need it as a double.
// (If it's already a double, skip these two steps)
var d = double.Parse(s);

// starting at the unix epoch
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

// simply add the number of seconds that you have
DateTime dt = epoch.AddSeconds(d);


Debug.WriteLine("{0} {1}", dt, dt.Kind);  // prints  05/12/2013 10:02:04 Utc

关于c# - 如何解析日期时间秒.ms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16521591/

相关文章:

c# - 窗体的透明背景图像 - 窗体的平滑边缘形状

c# - 使用 C# 从 MySQL 数据库中的 JSON 响应插入日期时间

c# - 我如何将 .net 的 HttpWebRequest 移植到 iOs 的 URLRequest

java - 如何解析嵌套的 XML 数据并仅从中提取用户标识?

C#逐行解析文本数据

algorithm - 转移和向前看之间的区别

php - 如何检查当前日期/时间是否超过设定的日期/时间?

python - 使用 pandas python 从文件中读取特定日期行

c# - 最好将错误代码/消息保存在数据库或字典中?

c# - 未生成捆绑查询 token