PC Review


Reply
Thread Tools Rate Thread

Difference in date time

 
 
Peter
Guest
Posts: n/a
 
      7th Jan 2005
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

 
Reply With Quote
 
 
 
 
Dan Bass
Guest
Posts: n/a
 
      7th Jan 2005
this is more readable than using Ticks...
TimeSpace ts = date1.Subtract( date2 );


for Ticks see this:
http://msdn.microsoft.com/library/de...tickstopic.asp
The value of this property is the number of 100-nanosecond intervals that
have elapsed since 12:00 A.M., January 1, 0001.



"Peter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
=?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=
Guest
Posts: n/a
 
      7th Jan 2005
Peter wrote:
> 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/
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Jan 2005
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
> this is more readable than using Ticks...
> TimeSpace ts = date1.Subtract( date2 );


And this is even more readable, IMO:

TimeSpan ts = date1-date2;

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Dan Bass
Guest
Posts: n/a
 
      10th Jan 2005
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.

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> <"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
>> this is more readable than using Ticks...
>> TimeSpace ts = date1.Subtract( date2 );

>
> And this is even more readable, IMO:
>
> TimeSpan ts = date1-date2;
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th Jan 2005
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Date and Time Difference Dale Fye Microsoft Access Queries 0 24th Sep 2008 05:16 PM
Difference between Windows Time Service and Internet Time in Date & Time Properties Saucer Man Windows XP General 0 14th Feb 2008 01:51 PM
Comparing 2 files on date/time stamp, and based time difference do a subroutine info@stevik.nl Microsoft Excel Programming 1 28th Sep 2007 03:53 AM
How can I find records based upon a date and time range while accounting for time zone difference BJC Microsoft Access 2 11th Jul 2007 01:13 AM
Calculating Difference Between Start Date & Time And End Date & Ti =?Utf-8?B?U2Ftd2Fy?= Microsoft Excel Misc 2 19th Dec 2005 12:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:54 PM.