DateTime Question

Hello Everyone,

I want to check if certain time is between these two times...How should i Do it...

Say If I want to check

DateTime.Now.TimeOfDay is between 7 AM -- 7PM.....

How can I do it

Any suggestions...

Thanks,

Harsimrat



Answer this question

DateTime Question

  • Tallgoose

    There are a number of possibilites, depending on exactly what you want checked. For what you're asking, I'd take do

    if (DateTime.Now.Hour >= 7 and DateTime.Now.Hour < 19)

    You could also use the Compare method on DateTime.



  • Robert Horvick

    Hi Harsimrat,

    Try subtracting one TimeSpan from an other TimeSpan like:

    TimeSpan timeDiff = new TimeSpan(19, 0, 0) - DateTime.Now.TimeOfDay();

    if (timeDiff.Hours < 12)

    {

    // Time is between 7 AM -- 7PM

    }

    Sandor


  • DateTime Question