tags:

views:

111

answers:

4

Is there a nice concise way to write this logic in C#?

if ((DateTime.Now >= "8:00 AM") && (DateTime.Now < 5:00 PM))
{// do something}

I am making a demo app where I want to make something happen in the work day, but I don't want this code to stand out much (a lot of casting = bad). (Because I want my demo stuff to be easier to see.

+11  A: 

Well, you could do:

DateTime now = DateTime.Now;
if (now.Hour < 8 || now.Hour >= 17)

Note that I generally prefer to only use the DateTime.Now property once, copying the result into the local variable as above - that way you don't get odd possibilities due to the time changing between calls. Not a problem here, but it could be in other cases.

Another possibility is to use DateTime.TimeOfDay if you want to handle things that way. I think the above is about as simple as it gets though.

EDIT: Steven pointed out that I changed the && in your original logic to || - your original logic can never work, as it can never be before 8am and after 5pm. The above works for "if it's not in the working day" - if you want "if it is in the working day" you just need:

DateTime now = DateTime.Now;
if (now.Hour >= 8 && now.Hour < 17)
Jon Skeet
Steven Sudit
@Steven: I didn't even notice myself doing that... although I'm not sure your answer actually does what you want it to at the moment. It's possible that I've got the exact opposite of what's intended, but your answer will currently *only* execute from midnight to 8am. The 17 is irrelevant.
Jon Skeet
On a side note, I've commented before that your inhuman speed is due to being replaced by an expert system, theorizing that this happens to all SO users once they reach 10k. Guess I'll find out soon enough.
Steven Sudit
You are correct. I should have flipped *both* comparisons. I'll do that now.
Steven Sudit
@Steven: Take care when you flip the comparisons... I've just noticed a bug in my original version, due to "17:05" still having an hour of 17... note the new `>=` in both of my versions :)
Jon Skeet
Ok, now that mine actually works, I'm not sure whether it represents the original intent or yours does. Either way, we both had to fix it because it wasn't working.
Steven Sudit
You're right again: I changed it from `> 8` to `>= 8`.
Steven Sudit
I was just writing up that my now twice-corrected answer was finally right, because the OP wanted it to trigger during working hours. But, of course, I was Skeeted. Clearly, I *deserve* to be replaced by an expert system. It'll cut down on my pizza costs, anyhow.
Steven Sudit
The condition in the last example is always true...
Ben Voigt
Sander Rijken
In the interests of preserving his rep (and since I, too, will be a pod person soon enough), I've edited his answer to correct it.
Steven Sudit
Well, I hit 10,000 but I am most definitely not a pod person. I was not replaced in any way. Clearly, my fears were ridiculous.
Steven Sudit
@Steven: Doh, thanks :)
Jon Skeet
No problem. We expert systems have to stick together.
Steven Sudit
+4  A: 

I think what you want to do here is something like this:

var now = DateTime.Now;
if (now.Hour >= 8 && now.Hour < 17)
Steven Sudit
LukeH
@LukeH: Corrected. Although you did get Skeeted.
Steven Sudit
Edited again. Damn that Skeet!
Steven Sudit
I don't think it counts as Skeeting when Jon posts a buggy answer.
Ben Voigt
Scratch that, people around here have so much respect for him that they'll upvote even wrong answers.
Ben Voigt
His answers have occasionally been buggy, but I've yet to see one that's wrong, much less clueless. He's a safe bet. Which is to be expected, given that he runs on 256 cores.
Steven Sudit
A: 

If you can format a string in the right format for the current date and start / end times you can use something like this.

DateTime now = DateTime.Now;
DateTime start = Convert.ToDateTime(now.Date.ToShortDateString() + " 8:00:00 AM");
DateTime end = Convert.ToDateTime(now.Date.ToShortDateString() + " 5:00:00 PM");
if (now >= start && now < end)
{

}

I still think it's cleaner to use the hour method posted by others, but this is a method that provides an answer similar to how you asked your question.

Wil P
+1  A: 

If you wanted to make it "pretty" you could also use an extension method:

public static class DateTimeHelper
{
    public static DateTime Time(this string time)
    {
        DateTime theTime = DateTime.Parse(time);
        return theTime;
    }
}

...

   if (DateTime.Now < "8:00 AM".Time() && DateTime.Now > "5:00 PM".Time())
   {
        // do something
   }
BrokenGlass
Only I'd call it `TimeOfDay`.
Steven Sudit
At risk of explaining my own attempts at humor, there's already a `TimeOfDay` property, which returns a `TimeSpan`. See http://msdn.microsoft.com/en-us/library/system.datetime.timeofday.aspx
Steven Sudit