Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys i want to find the total number of milliseconds between 2 time. I do a conversion on the first time to put in the correct format, then i proceed to to the calculation. However my result if very small and i can't seem to find out whats wrong.
C#
   string dateValues =  "10042011" ;
   string pattern = "MMddyyyy";
   DateTime parsedDate=new DateTime();


if (DateTime.TryParseExact(dateValues, pattern, null, DateTimeStyles.None, out parsedDate))
  {
       Console.WriteLine("Converted to this: " + parsedDate);
   }

   DateTime d1 = parsedDate;
   DateTime d2 = new DateTime(1970, 1, 1);

   TimeSpan ts = new TimeSpan(d1.Ticks - d2.Ticks);


   Console.WriteLine("Converted to this: " + ts);


the output is : 15982.00:00:00, and i am very sure this not correct.

thanks for your help
Posted
Comments
Sergey Alexandrovich Kryukov 4-Oct-13 15:10pm    
I cannot get it: are you trying to write something without looking into documentation? If so, how you do it? By intellisense? or by guessing?
—SA
Silvabolt 4-Oct-13 15:17pm    
FYI, I got 15251 instead of 15982, so you must have changed your date. And this is correct. This represents the total number of days between the specified DateTimes

 
Share this answer
 
Drop the new stuff - the difference between two DateTime objects is a Timespan already:
C#
TimeSpan ts = d1 - d2;
Console.WriteLine("Converted to this: " + ts.TotalMilliseconds);
 
Share this answer
 
Your code looks... well... If you take a date and subtract a date from it, you get a TimeSpan object back, not a date. You don't have to create a new Timespan. Your code should be closer to:
DateTime beginDate = new DateTime(1970, 1, 1);
DateTime endDate = DateTime.Now;

TimeSpan difference = endDate - beginDate;

Debug.WriteLine(difference.TotalMilliseconds);
 
Share this answer
 
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:
C#
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:
C#
if (DateTime.TryParseExact(dateValues, pattern, null, System.Globalization.DateTimeStyles.None, out parsedDate))
{
    Console.WriteLine("Converted to this: " + parsedDate);
}
else
{
    // report error: stop further execution in this method
    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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900