Difference in date time

P

Peter

Hi,

I want to calculate the time difference (in seconds,
minutes, days, etc.) between to DateTime values.

I expected some of the DateTime methods could do the job for
me, but after some search I came across the System.TimeSpan
method.

Following code seems to work correctly ..
string ss="2005-04-15 13:10:00";

string tt="2005-04-14 12:00:00";

date1 = DateTime.Parse(ss);

date2 = DateTime.Parse(tt);

System.TimeSpan ts = new
System.TimeSpan(date1.Ticks-date2.Ticks);

double min = ts.TotalMinutes;


Then the result for "min" equals 1510 minutes, what's
correct. But what is a tick in this context ?

Is this the "standard" method for this kind of calculation,
or is there an easier way to do this ?

Thanks !
Peter
 
?

=?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=

Peter said:
Hi,

I want to calculate the time difference (in seconds,
minutes, days, etc.) between to DateTime values.
(abridged)
Then the result for "min" equals 1510 minutes, what's
correct. But what is a tick in this context ?

Is this the "standard" method for this kind of calculation,
or is there an easier way to do this ?
1510 minutes is correct. There are 1440 minutes in a day. When you add
1440 + 60 + 10 you get 1510. Substracting one DateTime form another is
the "standard" way to find the difference between two points in time.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
J

Jon Skeet [C# MVP]

this is more readable than using Ticks...
TimeSpace ts = date1.Subtract( date2 );

And this is even more readable, IMO:

TimeSpan ts = date1-date2;
 
D

Dan Bass

The one thing that confuses me sometimes is whether objects have an
overloaded operator like this or not. I think it's probably more to do with
the way the IDE presents this.
For example, in this case you've got completion intellisense type drop down
on the "." key press after date1... where as if you press the "-", it would
be nice to know what you can subtract from that object, and what the
resulting object would be. Just an idea.
 
J

Jon Skeet [C# MVP]

The one thing that confuses me sometimes is whether objects have an
overloaded operator like this or not. I think it's probably more to do with
the way the IDE presents this.
For example, in this case you've got completion intellisense type drop down
on the "." key press after date1... where as if you press the "-", it would
be nice to know what you can subtract from that object, and what the
resulting object would be. Just an idea.

Yes, possibly. Personally I think these things should be used *very*
sparingly. The operators with DateTime/TimeSpan and the compiler
support provided for string concatenation are handy, but I'd think long
and hard before overloading operators myself.
 

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