Note: if you wonder why I posted this response when it appears that I am repeating answers already given by my esteemed colleagues, Comrade Sergey, Pyschic OriginalGriff, and others: I began this when there were no responses, and "something came up" that required me to be busy for a few hours. When I came back, I finished up the response, and posted it without checking to see if there were answers already. I'll leave it up, anyway. bw
You are actually correctly calculating the difference between the dates in Ticks: the value of 'ts you are seeing, 15982.00:00:00, is the default format for representing a TimeSpan: days.hours:minutes:seconds
If you want to see the number of Ticks:
Console.WriteLine("Converted to this: " + ts.Ticks);
You can work directly in Milliseconds here:
double TotalMilliseconds = (parsedDate - d2).TotalMilliseconds;
Console.WriteLine(TotalMilliseconds);
Note that in your code you "do the right thing" to transform your date in MMDDYYYY format into YYYYMMDD format, but you don't handle any possible error. I'd suggest:
if (DateTime.TryParseExact(dateValues, pattern, null, System.Globalization.DateTimeStyles.None, out parsedDate))
{
Console.WriteLine("Converted to this: " + parsedDate);
}
else
{
Console.WriteLine(""error in parsing date to YYYYMMDD format");
return;
}
By the way, you can "get away with" using #0 in the call to 'TryParseExact, instead of System.Globalization.DateTimeStyles.None, but I think it is much better practice to write it out in full, as you did: unless, of course, you have a 'using statement for System.Globalization.
If you want to get a sense of the controversy about calculating month/day differences between dates in .NET, check out: [
^]. The link is to an analysis of the various answers on the thread; reading the thread, itself, is fascinating.