Comparison of dates using Timespan.Compare()

Asked

Viewed 26 times

0

I’m trying to see if a schedule exists within a range of initial and final hours, but I’m having a problem setting the condition.

Example

Parameter time: 07:00:00

Initial time: 06:00:00

Final time: 23:00:00

Only by my code this parameter does not enter the range, and what really happens is that it reverses the entries in the condition, when it should enter, it leaves and vice versa.

Follows code:

  DateTime dtImportacao = new DateTime(2019,07,01,07,00,00);

            TimeSpan[] datasInicias = new TimeSpan[]
            {

            new TimeSpan(06,00,00),
            new TimeSpan(00,00,00),
            new TimeSpan(23,00,00),
            new TimeSpan(06,00,00),
            new TimeSpan(19,00,01),
            new TimeSpan(06,00,00),
            new TimeSpan(23,00,00),
            new TimeSpan(08,00,00),
            new TimeSpan(19,00,00),
            new TimeSpan(19,00,00),
            new TimeSpan(06,00,00),
            new TimeSpan(22,00,00),
            new TimeSpan(23,00,00),
            new TimeSpan(06,00,01)
        };
            TimeSpan[] datasFinais = new TimeSpan[]
            {
            new TimeSpan(23,00,00),
            new TimeSpan(23,59,59),
            new TimeSpan(06,00,00),
            new TimeSpan(19,00,00),
            new TimeSpan(05,59,59),
            new TimeSpan(23,00,00),
            new TimeSpan(06,00,00),
            new TimeSpan(20,00,00),
            new TimeSpan(06,00,00),
            new TimeSpan(05,45,59),
            new TimeSpan(22,00,00),
            new TimeSpan(06,00,00),
            new TimeSpan(06,00,00),
            new TimeSpan(22,59,59)
            };




            for (int i = 0; i < datasInicias.Length; i++)
            {


                if ((TimeSpan.Compare(dtImportacao.TimeOfDay, datasInicias[i]) == -1 || TimeSpan.Compare(dtImportacao.TimeOfDay, datasInicias[i]) == 0) ||  (TimeSpan.Compare(dtImportacao.TimeOfDay, datasFinais[i]) == 0 || TimeSpan.Compare(dtImportacao.TimeOfDay, datasFinais[i]) == 1))
                {
                    Console.WriteLine("Parametro de Comparação: {0}   | {1}  Até {2} Dentro do Range", dtImportacao.ToString("HH:mm:ss"), datasInicias[i], datasFinais[i]);
                }
                else
                {
                    Console.WriteLine("Parametro de Comparação: {0}   | {1}  Até {2} Fora do Range", dtImportacao.ToString("HH:mm:ss"), datasInicias[i], datasFinais[i]);
                }
            }

However I do not know should put in my decision all parameters of Timespan.Compare() to validate.

1 answer

1


Considering the above values:

Parameter time: 07:00:00

Initial time: 06:00:00

Final time: 23:00:00

You need your if to be ((07 >= 06) && (07 <= 23)) changing this to your variables and using Timespan.Compare(), I believe you need something like this:

for (int i = 0; i < datasInicias.Length; i++)
{
    if ((TimeSpan.Compare(dtImportacao.TimeOfDay, datasInicias[i]) == 1 || TimeSpan.Compare(dtImportacao.TimeOfDay, datasInicias[i]) == 0) &&  
    (TimeSpan.Compare(dtImportacao.TimeOfDay, datasFinais[i]) == -1 || TimeSpan.Compare(dtImportacao.TimeOfDay, datasFinais[i]) == 0))
    {
        Console.WriteLine("Parametro de Comparação: {0}   | {1}  Até {2} Dentro do Range", dtImportacao.ToString("HH:mm:ss"), datasInicias[i], datasFinais[i]);
    }
    else
    {
        Console.WriteLine("Parametro de Comparação: {0}   | {1}  Até {2} Fora do Range", dtImportacao.ToString("HH:mm:ss"), datasInicias[i], datasFinais[i]);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.