Click here to Skip to main content
12,450,977 members (23,823 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.7K views
2 bookmarked
Posted

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

You may also be interested in...

Pro
Pro

Comments and Discussions

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