Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how is checking dates in C#

Tags:

c#

i have 2 dates : date1 and date2 ; I want to check is that another date is between date1 and date2 thanks very much

like image 997
user1378024 Avatar asked Feb 20 '26 00:02

user1378024


1 Answers

You can just use the standard <, >, >= and <= operators:

if( someDate >= date1 && someDate <= date2 )
{
}

And, you can make your own extension method for it:

public static class DateExtensions
{
    public static bool Between( this DateTime d, DateTime start, DateTime end )
    {
        return d >= start && d <= end;
    }
}

Which you can use like this:

DateTime someDate = new DateTime (2012, 5, 6);

if( someDate.Between (date1, date2) )
{
    ...
}
like image 198
Frederik Gheysels Avatar answered Feb 21 '26 13:02

Frederik Gheysels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!