0

i am trying to check if the current time falls between 6 PM (evening) - 7 AM ()(next day), if current time falls between then i have to send mail:

below is my code:

    private static TimeZoneInfo pacific_Standard_Time = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
    private static TimeSpan Stime = DateTime.Parse("6:00:00 PM").TimeOfDay; //set start time
    private static TimeSpan Etime = DateTime.Parse("7:00:00 AM").TimeOfDay; //set End time

    private static string getSenderEmailID()
    {
        String strEmail = "";
        DateTime dateTime_Time = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, pacific_Standard_Time);  //get current time

//Check if current time falls between 6PM to 7AM
        if ((dateTime_Time.TimeOfDay > Stime) && (dateTime_Time.TimeOfDay < Etime))
        {
            strEmail = ConfigurationManager.AppSettings["NewUserEmailRedirectTo"];
        }
        else
        {
            strEmail = ConfigurationManager.AppSettings["NewUserEmailTo"];
        }
        return strEmail;
    }

but my code not checking if current time falls between 6 PM (day evening)- 7 AM (next day morning), i do not have much experience in C#, please let me know where i am going wrong

user2813740
  • 517
  • 1
  • 7
  • 29
  • 1
    what is "not working "? On what value of dateTime, you end up in the wrong branch of your if ? – Holger Jan 14 '20 at 13:24
  • 1
    Does this answer your question? [Find if current time falls in a time range](https://stackoverflow.com/questions/1504494/find-if-current-time-falls-in-a-time-range) – Polyfun Jan 14 '20 at 13:26
  • 1
    I have run your code. Everything works for me. – Krivitskiy Grigoriy Jan 14 '20 at 13:29
  • no not working if at all my current time falls between 6 PM - 7 AM then only statement should be true.. – user2813740 Jan 14 '20 at 13:32
  • check the answers, this is like you searching for a year falling between 2010 and 1950, by testing "if myyear > 2010". Startdate has to be the smaller date. – Holger Jan 14 '20 at 13:37
  • @Holger he means if the time falls between `day n at 6 PM` and `day n + 1 at 7 AM` – Cid Jan 14 '20 at 13:39
  • @Cid yes exactly.... day n 6 PM and next day 7AM ..i have to check if current time of the system if falls between these time span then redirect mail to other person or else send to main person. – user2813740 Jan 14 '20 at 13:47
  • Well, so you search for a time before 7Am OR after 6PM. But you have take a formula for in-between. you got the correct versions below. – Holger Jan 14 '20 at 13:49

2 Answers2

3

An hour can't be at the same time (no pun intended) greater than an afternoon hour and lesser than another morning hour.

In example, pseudo code

//         true               &&            false               == false
"08:00:00 PM" > "06:00:00 PM" && "08:00:00 PM" < "07:00:00 AM"

Will always evaluate to false ("08:00:00 PM" > "07:00:00 AM" is false), the same way than

//         false              &&            true               == false
"05:00:00 AM" > "06:00:00 PM" && "05:00:00 AM" < "07:00:00 AM"

Will always be false ("05:00:00 AM" > "06:00:00 PM" being false)

Change your condition to :

// Check this ------------------------VV
if ((dateTime_Time.TimeOfDay > Stime) || (dateTime_Time.TimeOfDay < Etime)) { ... }
Cid
  • 14,968
  • 4
  • 30
  • 45
  • that's very attentive – Holger Jan 14 '20 at 13:35
  • yhea this does make statement true.... is this best solution as per my requiremnts? as i have been told to check if day n 6 PM and next day 7AM ..i have to check if current time of the system if falls between these time span then redirect mail to other person or else send to main person. – user2813740 Jan 14 '20 at 13:53
  • i have to wait for time to be change on my client site and see if solution works for time between 6PM (current day) to 7AM next day. this will repeat everyday – user2813740 Jan 14 '20 at 13:57
  • @user2813740 okay, feel free to give me a feedback – Cid Jan 14 '20 at 13:58
  • no this solution not working as on my client site right now timing is 6.12 AM and statement became true ...it should fail – user2813740 Jan 14 '20 at 14:13
  • 6:12 AM is between 6PM and 7AM – Cid Jan 14 '20 at 14:15
  • if ((dateTime_Time.TimeOfDay > Stime) || (dateTime_Time.TimeOfDay < Etime)) Stime ==> 18:0:0 and ETime ==> 07:00:00 and dateTime_Time.TimeOfDay is 06:12:54 – user2813740 Jan 14 '20 at 14:16
  • private static TimeSpan Stime = DateTime.Parse("6:00:00 PM").TimeOfDay; //set start time ===> 18:0:0 private static TimeSpan Etime = DateTime.Parse("7:00:00 AM").TimeOfDay; //set End time ===> 07:00:00 do i have issue in 24 formate? i mean it is comparing 12 formate to 24 formate? – user2813740 Jan 14 '20 at 14:18
  • @user2813740 haha no worries – Cid Jan 14 '20 at 14:20
  • Hello, its 7:13 AM and my condition is failing......will have check again for tomorrow 6PM. thanks its is working. i will test 100% :-) – user2813740 Jan 14 '20 at 15:14
  • 1
    Hello, successfully integrated with existing code on client side and its working fine. thanks a lot for help, very much appreciated. – user2813740 Jan 30 '20 at 06:18
0

There are a few things that are unclear with your question. On the surface of it though, your if condition essentially reads as "Time_Of_Day is after 6pm and before 7am". This condition will never be true as is.

If you are looking at one day then what you want is "Time_Of_Day is after 6pm or before 7am" and your condition should be written as (dateTime_Time.TimeOfDay > Stime) || (dateTime_Time.TimeOfDay < Etime)

More likely though, what you are trying to achieve is a check that spans days. What you would want in that case is "Time_Of_Day is after 6pm today or before 7am tomorrow". In that case your condition should look like

private static DateTime Stime = DateTime.Now.Date.AddHours(18);
private static DateTime Etime = DateTime.Now.Date.AddDays(1).AddHours(7);

...

if((dateTime_Time> Stime) && (dateTime_Time < Etime))
{
...
}
peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76
  • no not at all system giving error.....my requirement is as below – user2813740 Jan 14 '20 at 13:48
  • day n 6 PM and next day 7AM ..i have to check if current time of the system if falls between these time span then redirect mail to other person or else send to main person. – user2813740 Jan 14 '20 at 13:48