Get hours difference between two DateTime vars?

R

Randall Parker

I know I could just get the year,month,day, etc of of each DateTime vars and calc the
hours difference between them. I've done this before in other languages with a
formula for handling leap years. But has someone already done what is necessary to
get the number of seconds or minutes or hours between two DateTimes?

Can one convert to ticks, do a tick difference, and then somehow convert to number of
hours or minutes or seconds that equals?

If so, what is ratio of ticks to some other time unit?
 
J

Joe

Hi Randall,

Is this what you're looking for?

DateTime start = new DateTime(2000, 1, 31);
TimeSpan ts = DateTime.Now.Subtract(start);

ts.TotalHours;

-Joe
 
J

Jon Skeet [C# MVP]

Joe said:
Hi Randall,

Is this what you're looking for?

DateTime start = new DateTime(2000, 1, 31);
TimeSpan ts = DateTime.Now.Subtract(start);

ts.TotalHours;

Or even more readably, for the middle line:

TimeSpan ts = DateTime.Now - start;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top