简体   繁体   English

如何将毫秒中的时间字符串转换为c#中的DateTime对象?

[英]How to convert time string in milliseconds to DateTime object in c#?

I have the time in milliseconds as a string like this: 229935440730121 How to convert this string to a DateTime object. 我有毫秒这样的字符串,以毫秒为单位: 229935440730121如何将此字符串转换为DateTime对象。
Thanks 谢谢

Update 更新

Sorry, The correct string is : 1423509923000 and should be 2/9/15 7:25:23 PM after conversion. 抱歉,正确的字符串是: 1423509923000 ,转换后应为15/2/9/15 7:25:23 PM。 It's a time string received from an telemetry device. 这是从遥测设备收到的时间字符串。

Milliseconds is a duration , not a time . 毫秒是持续时间 ,而不是时间 You can convert it to a TimeSpan easily: 您可以轻松地将其转换为TimeSpan

string ms = "229935440730121";
TimeSpan ts = TimeSpan.FromMilliseconds(double.Parse(ms));

To convert to a DateTime you need to know the reference point from which the span was measured, then just add the TimeSpan to that date: 要转换为DateTime您需要知道测量跨度的参考点,然后将TimeSpan添加到该日期:

DateTime dt = DateTime.MinValue;  // for example only
dt += ts;  // add the timespan to the date

In your example, that number of milliseconds represents over 7,280 years , so it's not clear what the reference point should be. 在您的示例中,该毫秒数表示超过7,280年 ,因此尚不清楚参考点应该是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM