Timer.Elapsed on Date Change (millsecs)

  • Thread starter Thread starter rhaley
  • Start date Start date
R

rhaley

ugh. Okay, I need to figure out the number of milliseconds between
DateTime.Now and the end of the day so that I can make changes based on
the date roll-over. I need to implement a Timer so that I can process
things on the Timer.Elapsed event that would happen right at the date
change (unless there is a better suggestion to implement an event on
the date change). DateTime isn't so friendly to this calculation.
Please, any suggestions are welcome. Preferably something efficient
and Threadsafe. VB.net is fine but C# is my current language.

Cheers and Thank you. This is bugging me!

Russ
 
It all depends on what you mean by 'right at the date change'.

If you mean within a few seconds of midnight then it is relatively easy.

Create a timer with an interval of, say, 10 seconds.

Directly before starting the timer, store Now in a variable with scope
sufficient so that the timer elapsed event handler can see it.

In the timer elapsed event handler if the date part of Now is different than
the date part of the stored value then the date has changed.
 
Thanks for the suggestion. A friend sent me this:

public long MillisecsToNextDay()
{

DateTime dteCurrent = DateTime.Now;
DateTime dteTomorrow = DateTime.Today.AddDays(1);

long lgnValue = dteTomorrow.Ticks - dteCurrent.Ticks;

return (lgnValue/1000);

}


You could also use the number of Ticks in a day (86400000000) and do
something like this:

86400000000 - DateTime.Now.Ticks

Cheers!
Russ
 
oops. scratch the last part of that suggestion (86400000000 -
DateTime.Now.Ticks )...

Russ
 
Back
Top