Get hours difference between two DateTime vars?

  • Thread starter Thread starter Randall Parker
  • Start date Start date
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?
 
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
 
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

Similar Threads

DateTime Span 6
Expand Range into List 2
Excel Convert Datetime Stamp to HH and MM Worked 7
DateTime span 2
Compare datetime in milliseconds 3
Comparing Dates 2
Difference in date time 5
DateTime Equals method & Ticks 11

Back
Top