DateTime丢失毫秒 [英] DateTime losing Milliseconds

查看:258
本文介绍了DateTime丢失毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文件中的日期解析为字符串,并将其转换为 DateTime ,以便可以将其与以前的日期进行比较。执行此操作时,我失去了毫秒,这非常重要,因为我正在解析的文件中缺少行。



从文件中提取日期的示例: 2014/11/12 10:47:23.571
m_LatestProcessDate 在我执行ParseExact 之后2014年12月11日10:47:23



请参阅下面使用的代码行。有什么想法可以解决这个问题吗?

  DateTime m_LatestProcessDate = DateTime.ParseExact(m_CurrentDateString,
yyyy / MM / dd HH:mm:ss.fff ,
CultureInfo.InvariantCulture);


解决方案

CodeCaster已经,不带任何参数,此方法使用 您的标准格式。 110%29.aspx rel = nofollow noreferrer> CurrentCulture



此说明符将您的 DateTime 格式化为 ShortDatePattern + LongTimePattern 。并且没有文化 LongTimePattern 上具有毫秒的部分。



例如; InvariantCulture 具有 HH:mm:ss 格式,不代表毫秒部分。



您可以看到 m_LatestProcessDate 的毫秒数;

  Console.WriteLine(m_LatestProcessDate.ToString( fff)); //打印571 


I am parsing a date from a file as a string and converting it to DateTime so that I can compare it with a previous date. I am losing the milliseconds when I do this which are extremely important as I am missing lines from the file I am parsing.

Example of Date From File I am extracting: 2014/11/12 10:47:23.571 m_LatestProcessDate after I do ParseExact 12/11/2014 10:47:23

See below line of code I am using. Any ideas how I get around this?

DateTime m_LatestProcessDate = DateTime.ParseExact(m_CurrentDateString,
                                                   "yyyy/MM/dd HH:mm:ss.fff",
                                                   CultureInfo.InvariantCulture);

解决方案

CodeCaster already explained your problem, I wanna add this as an answer if he let's me..

Don't worry! Your milliseconds didn't lost. They are still there. Sounds like you just didn't see them when you try to represent your m_LatestProcessDate and that's probably because your string representation of your DateTime doesn't have milliseconds part.

For example, if you use DateTime.ToString() method without any parameter, this method uses "G" standard format of your CurrentCulture.

This specifier formats your DateTime as ShortDatePattern + LongTimePattern. And no culture have Millisecond part on it's LongTimePattern.

For example; InvariantCulture has HH:mm:ss format which doesn't represent millisecond part.

You can see your milliseconds of your m_LatestProcessDate like;

Console.WriteLine(m_LatestProcessDate.ToString("fff")); // prints 571

这篇关于DateTime丢失毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆