DateTime plays milliseconds

I am parsing a date from a file as a string and converting it to in DateTime

order to compare it to a previous date. I lose milliseconds when I do this, which is very important as I am skipping lines from a file that I am parsing.

Example date from file I extract: 2014/11/12 10:47:23.571

m_LatestProcessDate

after ParseExact is executed12/11/2014 10:47:23

See below for the line of code I'm using. Any ideas how I get around this?

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

      

+2


source to share


1 answer


CodeCaster has already explained your problem, I want to add this as an answer if it allowed me ..

Do not worry! Your milliseconds are not lost. They are still there. It looks like you just didn't see them when you try to represent m_LatestProcessDate

yours, and it's probably because your string representation of yours DateTime

doesn't have a fraction of milliseconds.

enter image description here

For example, if you use DateTime.ToString()

method
without any parameter, this method uses the "G"

standard format of
your CurrentCulture

.



This specifier formats yours DateTime

as ShortDatePattern

+ . And no culture has a part on it . LongTimePattern

Millisecond

LongTimePattern

For example; InvariantCulture

has a format HH:mm:ss

that does not represent the millisecond portion.

You can see your milliseconds of yours m_LatestProcessDate

, for example:

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

      

+2


source







All Articles