DateTime.ParseExact省略在C#毫秒? [英] DateTime.ParseExact omitting milliseconds in c#?
问题描述
我试图将字符串转换为日期时间在C#。 字符串datestring = 2013年3月18日10:54:07.679
I am trying to convert a string to Datetime in c#. String datestring= 2013/03/18 10:54:07.679
-
我试过
DateTime的DT = DateTime.ParseExact(datestring,YYYY / MM / DD HH:MM:ss.fff格式,NULL);
其结果是{2013年3月18号上午10点54分07秒}
I tried
DateTime dt=DateTime.ParseExact(datestring, "yyyy/MM/dd HH:mm:ss.fff",null);
The result is {3/18/2013 10:54:07 AM}
我试过 DateTime.TryParseExact(datestring,YYYY / MM / DD HH:MM:ss.fff格式,CultureInfo.InvariantCulture,DateTimeStyles.None,出dttt);
其结果是{2013年3月18号上午10点54分07秒}
I tried DateTime.TryParseExact(datestring,"yyyy/MM/dd HH:mm:ss.fff",CultureInfo.InvariantCulture,DateTimeStyles.None,out dttt);
The result is {3/18/2013 10:54:07 AM}
在上述两种情况下,其ommitting millisecons(679)。
In both the above cases its ommitting millisecons(679).
怎么能正确地我转换为日期时间通过保持毫秒?
How can I convert it to datetime correctly by keeping the milliseconds?
推荐答案
这不是ommitting你只是通过检查调试器和调试器显示它使用是上午还是下午,它doenot显示毫秒组成部分。
it's not ommitting you are just checking through debugger and debugger shows it using AM or PM ,it doenot show milliseconds part.
试试这个:
DateTime dt=DateTime.ParseExact(datestring, "yyyy/MM/dd HH:mm:ss.fff",null);
Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss.fff"));
编辑:字符串从您的评论但我需要在日期时间的答案,而不是
您已经将DateTime包括毫秒
只是调试器不显示,因为
(正如中提到的评论蚂蚁P
)调试器调用的无参数的重载的ToString()
方法,它显示了<$ C C>日期时间 $没有毫秒。
You have already the DateTime including MilliSeconds
just debugger is not showing because
(As mentioned in comment by Ant P
) Debugger calls the Parameterless overload of ToString()
method which shows the DateTime
without MilliSeconds.
这篇关于DateTime.ParseExact省略在C#毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!