469,643 Members | 2,036 Online

# 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