Reputation: 19
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);
Upvotes: 2
Views: 5802
Reputation: 98750
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
Upvotes: 3