473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Difference in date time

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

Nov 16 '05 #1
5 11550
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" <An*****@work.nl> wrote in message
news:11**************@wgsvr01.wldelft.nl...
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

Nov 16 '05 #2
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/
Nov 16 '05 #3
<"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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
<"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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: akoper | last post by:
I have a table that is a project. Each record is a task in the project. One field in each record is a date/time stamp for when that task was completed. I need to be able to: 1) compute how much...
5
by: Gord | last post by:
Many scripts and calendars call client side system time in order to make presentations. However, the client's time may be improperly set, if set at all, and/or the relevant time may be from...
3
by: Reney | last post by:
I am using Access in my project. In one of the forms, I am calling two tables, and two of the columns have date/time type, namely "ClockIn" and "ClockOut". I created a dataset and filled the...
7
by: Big Tony | last post by:
I have a piece of code written in VS.NET 2003. It runs fine on one machine and does not on another. It throws an invalid cast exception. Any idea why there is a difference between machines? Dim...
7
by: Jerome | last post by:
Hallo, I know a lot has already been told about date/time fields in a database but still confuses me, specif when dealing with SQLserver(Express). It seems that sqlserver only accepts the date in...
4
by: sbowman | last post by:
I have a table with help desk ticketing information. There is a Date/Time open, Date/Time closed field both formatted as: MM/DD/YYYY hh:nn:ss I need to calculate the difference between these two...
4
by: lenygold via DBMonster.com | last post by:
I found this example in MYSQL: create table events ( id integer not null primary key , datetime_start datetime not null , datetime_end datetime not null ); insert into events values ( 1,...
4
by: ahmurad | last post by:
Dear Brothers, I am struggling the following four Date-Time type values which were inputted into MYSQL database in different tables. As MYSQL Default Time Format: YYYY-MM-DD HH:MM:SS, So I used...
1
by: crs27 | last post by:
Hai All, I wanted to know how can i get the difference between two Timestamp using some function in mysql and write a query. For eg:i have from date time = 01/01/2008 01:00:00 ...
0
by: Syed Khaleel Ahamed | last post by:
Dear Sir, I want to get the difference of date/time captured in mysql and present date/time in jsp. I am using the following code to get, but its getting difference only in hrs, i used...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.