473,785 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11593
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.n l> 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<"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.co m>
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.co m>
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
8592
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 time has passed between each date/time stamp and 2) compute how much time has passed between the first and last date/time stamp (total project time). This involves a self join, and I have nearly gotten it to work, but I need a little more help. ...
5
2311
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 another time zone. It would be of great value if the time of some central source could be drawn, in the form of a ".js" file, into the script to avoid dependence on the client's date/time.
3
7480
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 dataset already. But I need to add another column which should calculate the difference between these two columns. I don't know how to write code in the expression to accomplish this date difference calculation. Any help would be appreciated. Here is...
7
1662
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 CurDate As Date = Now Dim SpecTime As Date = "#07:08:30 PM#" Eception thrown here: CurDate = SpecTime + CurDate
7
4242
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 a "yyyyMMdd" format? (difference between Express and MSDE2000A ?) What is the one and only true way to deal with this problem in VB2005: Local settings are Dutch (Belgium) ; thus date is in "dd/MM/yy" (or perhaps dd/MM/yyyy) and time in...
4
6359
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 times and then calculate the average of the difference. How do I go about this??? Help is much appreciated... Shelley
4
50194
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, '2006-09-09 14:00', '2006-09-09 16:00' ) ,( 2, '2006-09-10 09:00', '2006-09-10 17:00' ) ,( 3, '2006-09-11 13:30', '2006-09-11 14:45' )
4
32957
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 the MYSQL Tables DateTime Type as Text. Time-formet 1. 04:02:27 16/01/2009 Time-formet 2. 16/01/2009 13:53:19 Time-formet 3. 00901E+13 Time-formet 4. Wed Jan 14 00:09:09 BDT 2009
1
3632
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 to date time = 01/05/2008 19:00:00 result is = 4 months 18 hrs How do i get the difference i wanted that in year,days,hours,min?
0
1746
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 split strings in jsp for that, can u please help me in this code, i have to get difference in min and sec also. <%@ page import="java.sql.*,java.text.SimpleDateFormat" %> <%! int x; %> <%
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.