473,795 Members | 3,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get Time Difference

RN1
A Form has 2 TextBoxes where in users enter date & time. Example (in
mm/dd/yyyy format):

09/20/2008 17:54 (1st TextBox)

09/29/2008 6:13 (2nd TextBox)

How do I find out how much time (in hours & minutes) has elapsed
between the 2 datetime values? I need the exact time difference.

Thanks.
Oct 4 '08 #1
3 3945
"RN1" <rn**@rediffmai l.comwrote in message
news:be******** *************** ***********@a2g 2000prm.googleg roups.com...
How do I find out how much time (in hours & minutes) has elapsed
between the 2 datetime values? I need the exact time difference.
http://www.google.co.uk/search?sourc...23%22+TimeSpan
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 4 '08 #2
RN1 wrote:
A Form has 2 TextBoxes where in users enter date & time. Example (in
mm/dd/yyyy format):

09/20/2008 17:54 (1st TextBox)

09/29/2008 6:13 (2nd TextBox)

How do I find out how much time (in hours & minutes) has elapsed
between the 2 datetime values? I need the exact time difference.

Thanks.
Use the DateTime.Parse or DateTime.ParseE xact method to parse the
strings into DateTime values, then you just subtract one from the other
to get a TimeSpan value. With the TimeSpan value you can use the Hours
and Minutes properties to get the hours and minutes.

--
Göran Andersson
_____
http://www.guffa.com
Oct 4 '08 #3
At its simplest, you can do this:

DateTime time1 = DateTime.Parse( TextBox1.Text);
DateTime time2 = DateTime.Parse( TextBox2.Text);

TimeSpan ts = time2 - time1;

int hours = ts.Hours + (ts.Days * 24);
int minutes = ts.Minutes;

This makes a lot of assumptions, however. Assumptions:

1. Textboxes have actual time values
2. Time2 is larger than Time1

If you can verify the assumptions will always be correct, you can roll with
this. If not, you need a safety net. It looks more like this:

DateTime time1, time2;
TimeSpan ts;

bool time1Okay = DateTime.TryPar se(TextBox1.Tex t, out time1);
bool time2Okay = DateTime.TryPar se(TextBox2.Tex t, out time2);

if ((time1Okay) && (time2Okay))
{
int compare = DateTime.Compar e(time1, time2);

if (compare < 0)
{
//time 1 is smaller than time 2
ts = time2 - time1;
}
else
{
//time 1 is bigger than time 2
ts = time1 - time2;
}

//Calculate values
int hours = ts.Hours + (ts.Days * 24);
int minutes = ts.Minutes;

//Display Differences here
}
else
{
//Alert user time value(s) is/are bad
}

If you also want seconds, you can do this:

//Calculate values
int hours = ts.Hours + (ts.Days * 24);
int minutes = ts.Minutes;
int seconds = ts.Seconds;

But perhaps you like to go about this the hard way?

double totalSeconds = ts.TotalSeconds ;
double workingMinutes = (totalSeconds / 60);
int seconds = (int)(workingMi nutes - (int)workingMin utes);
int hours = (int)(workingMi nutes / 60);
int minutes = (int)(workingMi nutes - (hours *60));

You can also go from the other direction, but you will end up with rounding
errors:

double totalHours = ts.TotalHours;
int hours = (int)totalHours ;
double workingMinutes = (totalHours - hours) * 60;
int minutes = (int)workingMin utes;
int seconds = (int)((workingM inutes - minutes) * 60);

Have fun!

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

*************** *************** **************
| Think outside the box! |
*************** *************** **************
"RN1" <rn**@rediffmai l.comwrote in message
news:be******** *************** ***********@a2g 2000prm.googleg roups.com...
>A Form has 2 TextBoxes where in users enter date & time. Example (in
mm/dd/yyyy format):

09/20/2008 17:54 (1st TextBox)

09/29/2008 6:13 (2nd TextBox)

How do I find out how much time (in hours & minutes) has elapsed
between the 2 datetime values? I need the exact time difference.

Thanks.
Oct 4 '08 #4

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

Similar topics

2
9783
by: Jason Reljac | last post by:
Howdy, For a project I am working on right now I need to be able to calculate the time difference between to given times. (For example...The difference between 11:30 am and 1:45pm being 2:15) Right now the input is setup as 2 text boxes, 2 dropdowns, and 2 radio groups. The text boxes get the hour entered into them, the drop downs holdd a list contaning 00, 15, 30, and 45 (the minutes) and the radio groups being am or pm.
10
24497
by: Andreas | last post by:
Hi! Is it possible to get a time event at a specific time, for instance eight a'clock? My program is running in the background and is minimized to the tray bar. If not, is there a smooth way to accomplish this in a different way? Best regards, Andreas Lundgren
2
3042
by: Joe User | last post by:
I am looking to calculate the difference between and event time and a sample time of Now. This is the query that I thought would do it, however I'm returning DIFFERENCE values that look the same when the calcuation is being made on different EVENT_TIME values.... I thought I knew how DateDiff worked, but apparently not. select GetDate()as NOW,event_time,Datediff(s,(Cast(event_time as Numeric)),(Cast(GetDate() as Numeric))) as...
6
3754
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to speed things up implementing some of the functions in C. So I need profiling. I first tried to use the default python profiler, but profiling my application multiplies the execution time by a factor between 10 and 100 ! So I decided to give a...
6
2728
by: Michael Bulatovich | last post by:
I have a very simple db I use for keeping track of hours, tasks, projects, clients etc. It has a form that I use to enter data. Currently the form has a textbox for a field called "start time", another for a field called "end time" and a third for a field called "hours", among other controls. I now type in the approximate times in the first two textboxes, and mentally calculate and enter the difference or elapsed time in the third. I...
7
8499
by: Edward Mitchell | last post by:
I have a number of DateTimePicker controls, some set to dates, some set to a format of Time. The controls are all embedded in dialogs. I created the controls by dragging the DateTime picker from the Toolbox and then set the Format property appropriately. I have noticed that sometimes the Time format will reset spontaneously to Short Date. I looked at the .rc file and found that the usual form for a Short Date is as follows: CONTROL ...
5
1469
by: Geoff Jones | last post by:
Hi I have question regarding times and dates in a datatable. I have one table with one column having the date e.g.03/09/04, and another column other the time 08:03:05. The other table has one column with the date/time e.g. 09/06/04 13:05:03. What is the easiest way for me to calculate the difference in time, in seconds, between the rows of each table?
3
23750
by: Randall Parker | last post by:
Suppose one has a database of UTC times that are from different dates in the past. The problem with translating those times to a local time is that one does not know for each UTC time whether the local time at that same moment had daylight savings time in effect. If one has a local time it is easier to translate it into UTC time. One can get the UTC time and one can even calculate the number of hours difference between them as follows: ...
3
3441
by: Steve | last post by:
I am trying to calculate elapsed travel times for flights. My plan is to enter the local departure time, the departure city and the local arrival time and city. These times would be standardised to GMT and a date difference calculated which would be the theoretical elapsed time of the flight I have one Table called Cities which has the name of the city and the difference from GMT eg Sydney is 10 (ie 10 hrs ahead of GMT), LA is -7 ie 7...
15
6442
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but but that only gave me difference in hours and only if the times were on the same day (after wrapping with date() function). TIA
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10213
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
10163
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
10000
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
9040
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
7538
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.