0

I have a problem when i am trying to compare start Hour with DateTime.Now.Hour and also the End Hour with DateTime.Now.Hour in order to get the Shift currently, so the problem is how to Compare time in 24 hour.

The problem in this code : taking 17:00 > 00:00 and this is wrong

public void GetShift()
    {
        DateTime S1_Start = Convert.ToDateTime("00:00");
        string S1_HourEnd = Convert.ToDateTime("00:00").AddHours(8).ToString("HH:mm");
        DateTime S1_End = Convert.ToDateTime(S1_HourEnd);

        DateTime S2_Start = Convert.ToDateTime("08:00");
        string S2_HourEnd = Convert.ToDateTime("08:00").AddHours(8).ToString("HH:mm");
        DateTime S2_End = Convert.ToDateTime(S2_HourEnd);

        DateTime S3_Start = Convert.ToDateTime("16:00");
        string S3_HourEnd = Convert.ToDateTime("16:00").AddHours(8).ToString("HH:mm");
        DateTime S3_End = Convert.ToDateTime(S3_HourEnd);


        if (S1_Start.Hour <= DateTime.Now.Hour && S1_End.Hour > DateTime.Now.Hour)
        {
            HourStart_Shift = new TimeSpan(S1_Start.Hour, 0, 0);
            HourEnd_Shift = new TimeSpan(S1_End.Hour, 0, 0);
            Current_Shift = "Shift_1";
        }

        if (S2_Start.Hour <= DateTime.Now.Hour && S2_End.Hour > DateTime.Now.Hour)
        {
            HourStart_Shift = new TimeSpan(S2_Start.Hour, 0, 0);
            HourEnd_Shift = new TimeSpan(S2_End.Hour, 0, 0);
            Current_Shift = "Shift_2";
        }

        if (S3_Start.Hour <= DateTime.Now.Hour && S3_End.Hour > DateTime.Now.Hour)
        {
            HourStart_Shift = new TimeSpan(S3_Start.Hour, 0, 0);
            HourEnd_Shift = new TimeSpan(S3_End.Hour, 0, 0);
            Current_Shift = "Shift_3";
        }
    }
  • 1
    I think you might need to formulate your question a little more clearly. I think you are maybe running into difficulties when you have 17:00 yesterday as a start and e.g. 03:00 today as an end date? What are you ultimately trying to calculate - is is the time between the start and end date? – Paddy Jul 27 '21 at 07:24
  • S3_Start and S3_End need to have a meaningful date part. – Steve Jul 27 '21 at 07:24
  • 1
    If you want `00:00 > 17:00`, then you need to operate with `DateTime` ([convert TimeSpan](https://stackoverflow.com/q/10276228/1997232)?) including `Date` component of it. This would allow `00:00 tomorrow > 17:00 today`. – Sinatr Jul 27 '21 at 07:25
  • Using Hour is truncating the day and giving error. If looks like you want to Now time to be the start hour. So use following : DateTime now = DateTimeNow; DateTime startHour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0); } Then : if (S3_Start <= startHour && S3_End > startHour) – jdweng Jul 27 '21 at 07:36
  • Can you please check the modification on my code, the solutions you suggest to me will not work for me :( – Hamza El Hammoudi Jul 27 '21 at 07:43
  • If you know that the endtime should always be later than the starttime, then add 24 hours when you find otherwise (but this will not work for timespans > 24h) – Hans Kesting Jul 27 '21 at 10:04

1 Answers1

1

This should be because 00:00 is the next day. Change 00:00 to 23:59

Itzuko
  • 54
  • 8