469,643 Members | 2,036 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

How to use Time Span more optimized

Hi IT's,

Directly i'm coming to the point. I have Schedule time, Interval and current time. What i want to do is I have to start the process using schedule time and intrevel,

For example The schedule time is 5 PM and Interval is 2 Hrs and Current time is 7:30 PM then what i have to do is i have to calculate the wait time to initiate the proces. In this case i have to start the process by 5 PM and next is 7 PM ann then 9 PM and so on... But now the time is 7:30 PM so i have to calculate the wait time as 1 Hour and 30 Minuts 01:30,

So i have already wrote the code for this but i want to know if there is any better logic...

Here is the code i wriiten...
string strCurTime = DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
Expand|Select|Wrap|Line Numbers
  1. TimeSpan currentTime = TimeSpan.Parse(strCurTime); // Current time
  2.  
  3. TimeSpan shedule_time = new TimeSpan(19, 30, 0); 
  4.  
  5. int nInterval = 2;  // Interval
  6.  
  7. TimeSpan ts = new TimeSpan(nInterval,0, 0 ); //Converting to 2 hrs
  8.  
  9. TimeSpan tTempTime = shedule_time; //Assigning shedule time to temp time stamp. 
  10.  
  11. while (tTempTime < currentTime) //if the shedule time is less than curr_time
  12. {
  13.      tTempTime += ts;  // Add interval hrs with the schedule time till it greater than current time
  14. }
  15.  
  16. TimeSpan tDiff = tTempTime - currentTime; // calculating time diff
  17.  
  18.                 while(tDiff > ts) // if difference grater than interval
  19.                 {
  20.                     tDiff -= ts; // subtract the intervel to calculate correct time to wait
  21.                 }
  22. //Now the tdiff hold 1 and half hours
  23.  
Pls help me out if there is any other good logic...
Dec 20 '07 #1
1 1220
Hi Durai,...............................
Dec 20 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Gerry | last post: by
4 posts views Thread by Gerry | last post: by
4 posts views Thread by John | last post: by
3 posts views Thread by DotNetGuy | last post: by
7 posts views Thread by David | last post: by
4 posts views Thread by =?Utf-8?B?TGUgSHVuZw==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.