471,605 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Time difference

Hi,

I need to calculate the time difference between today 2pm and tomorrow 2 am.
How do I calculate?

Thanks

Vivek
Dec 4 '05 #1
4 3244
You can add and subtract DateTime values and end up with a TimeSpan...
The following code is a very easy representation of how to use it:
DateTime dttoday = DateTime.Now(); // Today's Time
DateTime dttomorrow = DateTime.Now.AddDays(0.5); // 12 hours from now
TimeSpan ts = dttomorrow - dttoday; // Subtract today from "tomorrow"
Debug.WriteLine(ts.TotalHours.ToString()); // Write out hours difference

Vivek Sharma wrote:
Hi,

I need to calculate the time difference between today 2pm and tomorrow 2 am.
How do I calculate?

Thanks

Vivek

Dec 4 '05 #2
Also, Please disregard the () after .Now - I was writing too fast and
not thinking of what i was doing. It should really look like this:

DateTime dttoday = DateTime.Now;
DateTime dttomorrow = DateTime.Now.AddDays(0.5);
TimeSpan ts = dttomorrow - dttoday;
Debug.WriteLine(ts.TotalHours.ToString());
Vivek Sharma wrote:
Hi,

I need to calculate the time difference between today 2pm and tomorrow 2 am.
How do I calculate?

Thanks

Vivek

Dec 4 '05 #3
Thanks for your help... What shall I do in case it let us say 2.45pm and
next day 3.25pm. How do I handle this?

Thanks


"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:u6****************@TK2MSFTNGP10.phx.gbl...
Also, Please disregard the () after .Now - I was writing too fast and not
thinking of what i was doing. It should really look like this:

DateTime dttoday = DateTime.Now;
DateTime dttomorrow = DateTime.Now.AddDays(0.5);
TimeSpan ts = dttomorrow - dttoday;
Debug.WriteLine(ts.TotalHours.ToString());
Vivek Sharma wrote:
Hi,

I need to calculate the time difference between today 2pm and tomorrow 2
am. How do I calculate?

Thanks

Vivek

Dec 5 '05 #4
So the answer would be 24.6 hours right? You can aquire this
information by:

DateTime dttoday = DateTime.Parse(@"12/4/2005 2:45 PM");
DateTime dttomorrow = DateTime.Parse(@"12/5/2005 3:25 PM");
TimeSpan ts = dttomorrow - dttoday;
Debug.WriteLine(ts.Days.ToString() + "Days, " + ts.Hours.ToString() +
"Hours, " + ts.Minutes.ToString() + "Minutes");
Vivek Sharma wrote:
Thanks for your help... What shall I do in case it let us say 2.45pm and
next day 3.25pm. How do I handle this?

Thanks


"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:u6****************@TK2MSFTNGP10.phx.gbl...
Also, Please disregard the () after .Now - I was writing too fast and not
thinking of what i was doing. It should really look like this:

DateTime dttoday = DateTime.Now;
DateTime dttomorrow = DateTime.Now.AddDays(0.5);
TimeSpan ts = dttomorrow - dttoday;
Debug.WriteLine(ts.TotalHours.ToString());
Vivek Sharma wrote:
Hi,

I need to calculate the time difference between today 2pm and tomorrow 2
am. How do I calculate?

Thanks

Vivek


Dec 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jason Reljac | last post: by
2 posts views Thread by Joe User | last post: by
6 posts views Thread by Michael Bulatovich | last post: by
5 posts views Thread by Geoff Jones | last post: by
3 posts views Thread by Randall Parker | last post: by
3 posts views Thread by Steve | last post: by
15 posts views Thread by student4lifer | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

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.