• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 826
  • Last Modified:

Is it after midnight?

Using DateTime.Now how can I determine if it is after midnight?
Tom Knowlton
Tom Knowlton
3 Solutions
u can use DateTime.Now.Hour.ToString() and  see if it is less than 12
or u can use DateTime.TimeOfDay Property
it's always after midnight. if it's before midnight it's yesterday.

ok, now for a serious answer. the hour should be 1 for 1 am instead of 13 for 1 pm
Tom KnowltonWeb developerAuthor Commented:
I want to know if it is between 12 midnight and say, 1 AM.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tom KnowltonWeb developerAuthor Commented:
>>>> it's always after midnight. if it's before midnight it's yesterday.

:)  smart a**!
Tom KnowltonWeb developerAuthor Commented:
A little more info on this.

I have a phone log file which is updated will call information throughout the day.

Add midnight, I want to take all call information older than 30 days and append it to another table.

The append part is already done....I just want to make sure the timing is correct.

This only needs to fire ONCE a day....between midnight and 1 AM, for example.
You could use a separate time for this portion and just have it run once every 24 hours.

Otherwise you would want to do something like

// Initialize a default LastRun.  This will always run the first time.
// because the initialization will not be today.
DateTime LastRun = New DateTime();

in your timer function.

// Check if it's currently between midnight and 1 am.
if( DateTime.Now.Hour > 0 && DateTime.Now.Hour < 1  )
   // Make sure this has not run already today.
   if( DateTime.Now.Date > LastRun )
      LastRun = Datetime.Now;
      // run the append process.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now