2

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);
Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
Pauline
  • 19
  • 1
  • 2
  • 6
    How do you detect you lose milliseconds? Did you inspect the `m_LatestProcessDate.Millisecond` property, or just dump the DateTime to somewhere? The default DateTime format doesn't include milliseconds. – CodeCaster Nov 13 '14 at 11:28
  • 1
    Duplicate of [this question](http://stackoverflow.com/questions/23788656/datetime-parseexact-omitting-milliseconds-in-c) – Tea With Cookies Nov 13 '14 at 11:29

1 Answers1

3

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.

enter image description here

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
Community
  • 1
  • 1
Soner Gönül
  • 91,172
  • 101
  • 184
  • 324