Click here to Skip to main content
Click here to Skip to main content

Tagged as

Using DateTime and TimeSpan objects to test for a specific time interval.

, 16 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple way to determine when to execute something based on the local systems time using .NET DateTime and TimeSpan objects.
bool work = true;
//execute at 2:06 PM
TimeSpan timeToExecute = new TimeSpan(14,6,00);
do
{
  DateTime nowTime = DateTime.Now;
  TimeSpan systemTime = new TimeSpan(nowTime.TimeOfDay.Hours,
                                       nowTime.TimeOfDay.Minutes,
                                       nowTime.TimeOfDay.Seconds);
  Console.WriteLine(systemTime);
  Console.WriteLine(timeToStop);
 
  if(timeToExecute.CompareTo(systemTime) == 0)
  {
    //Execute whatever you need to here.
    Console.WriteLine("END");
  }
}while(work);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

claytoC001
Systems Engineer
United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 16 Feb 2011
Article Copyright 2011 by claytoC001
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid