ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

DateTimeCheckValve

this ExtensionMethod check if startdate is null reutrn the min value of datetime and if the end date is null return the max value

Source

public static class TimeExtension
{

    public static DateTime ReturnMinValueIfNull(this DateTime? time)
    {
        return time ?? DateTime.MinValue;
    }
    
    public static DateTime ReturnMaxValueIfNull(this DateTime? time)
    {
        return time ?? DateTime.MaxValue;
    }
}

Example

if startDate and endDate is null 
var startDate = someModel.startDate.ReturnMinValueIfNull(); //  1/1/0001 12:00:00 AM
        var endDate = someModel.endDate.ReturnMaxValueIfNull(); //  12/31/9999 11:59:59 PM

// if startDate and endDate not null it return the real value

Author: MasoudH

Submitted on: 10 okt. 2023

Language: csharp

Type: DateTime

Views: 1336