C# - Porównywanie dat

0

Witam
Nie mogę sobie poradzić z porównaniem dat w C#.
Jak mogę zrobić w C# np. takie proste porównanie jak poniżej?

Edit: to na dole to oczywiście nie C# tylko przykład co chciałbym uzyskać, a nie mogę.

If (czas1 >= "05:45") And (czas1 < "13:45") Then
    ktora_zmiana = "Zmiana nr 1"
ElseIf (czas1 >= "13:45") And (czas1 < "21:45") Then
    ktora_zmiana = "Zmiana nr 2"
ElseIf (czas1 >= "21:45") And (czas1 <= "23:59") Then
    ktora_zmiana = "Zmiana nr 3"
ElseIf (czas1 >= "00:00") And (czas1 < "05:45") Then
    ktora_zmiana = "Zmiana nr 3"
    Data1 = DateAdd("d", -1, data)
End If

Z góry dziękuję za podpowiedź.

0

Możesz skorzystać ze struktury TimeSpan i jej metody TimeSpan.Parse(string).

Albo możesz sobie zrobić swoją:

public struct Time : IComparable<Time>, IEquatable<Time>
{
    private readonly int _hour;
    private readonly int _minute;
    private readonly int _second;

    public Time(int hour, int minute, int second)
    {
        _hour = hour;
        _minute = minute;
        _second = second;
    }

    public Time(int hour, int minute) : this(hour, minute, 0)
    {
    }

    public static Time Parse(string input)
    {
        // TODO
        // parse from "XX:XX" and "XX:XX:XX"
        throw new NotImplementedException();
    }

    public static bool operator <(Time first, Time second)
    {
        // TODO
        throw new NotImplementedException();
    }

    public static bool operator <=(Time first, Time second)
    {
        // TODO
        throw new NotImplementedException();
    }

    public static bool operator >(Time first, Time second)
    {
        // TODO
        throw new NotImplementedException();
    }

    public static bool operator >=(Time first, Time second)
    {
        // TODO
        throw new NotImplementedException();
    }

    public static bool operator ==(Time first, Time second)
    {
        // TODO
        throw new NotImplementedException();
    }

    public static bool operator !=(Time first, Time second)
    {
        // TODO
        throw new NotImplementedException();
    }

    public int CompareTo([AllowNull] Time other)
    {
        // TODO
        throw new NotImplementedException();
    }

    public bool Equals([AllowNull] Time other)
    {
        // TODO
        throw new NotImplementedException();
    }

    public override bool Equals(object obj)
    {
        // TODO
        throw new NotImplementedException();
    }

    public override int GetHashCode()
    {
        // TODO
        throw new NotImplementedException();
    }
}
0

Cześć dzięki za odpowiedzi chyba już ogarniam temat :-)

0

Najłatwiej chyba byłoby porównać TotalHours, przykład:



        private static double ToTotalHour(int hour, int minute)
        {
            return hour + minute / 60.0d;
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            DateTime czas1 = DateTime.ParseExact("2021-02-02 13:50:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
            TimeSpan timeSpan = czas1.Subtract(czas1.Date);

            if (timeSpan.TotalHours >= ToTotalHour(5, 30) && timeSpan.TotalHours < ToTotalHour(13, 45))
            {
                //ktora_zmiana = "Zmiana nr 1"
            }
        }
0

Masz też DateTimeOffset.

1 użytkowników online, w tym zalogowanych: 0, gości: 1