C# Nullable Datetime

The nullable DateTime in C# allows you to assign null values to DateTime variables, which is useful when a date might not be available or applicable. This is particularly valuable in database operations where date fields can be NULL, or when dealing with optional date parameters.

Syntax

A nullable DateTime is declared using the question mark (?) syntax −

DateTime? variableName = null;

Alternatively, you can use the full Nullable<DateTime> syntax −

Nullable<DateTime> variableName = null;

Key Properties and Methods

Nullable DateTime provides several useful properties and methods −

  • HasValue − Returns true if the nullable contains a value, false if it's null

  • Value − Gets the actual DateTime value (throws exception if null)

  • GetValueOrDefault() − Returns the value or DateTime.MinValue if null

  • GetValueOrDefault(defaultValue) − Returns the value or specified default if null

Nullable DateTime States Has Value dt.HasValue = true dt.Value accessible Contains actual DateTime value Is Null dt.HasValue = false dt.Value throws error Use GetValueOrDefault() for safe access

Basic Usage Example

using System;

class Program {
    static void Main() {
        DateTime? dt = null;
        DateFunc(dt);
        
        dt = DateTime.Now;
        DateFunc(dt);
        
        dt = null;
        Console.WriteLine("GetValueOrDefault: " + dt.GetValueOrDefault());
        Console.WriteLine("GetValueOrDefault with custom: " + dt.GetValueOrDefault(new DateTime(2020, 1, 1)));
    }
    
    static void DateFunc(DateTime? dt) {
        if (dt.HasValue) {
            Console.WriteLine("Date: " + dt.Value);
        } else {
            Console.WriteLine("Date is null");
        }
    }
}

The output of the above code is −

Date is null
Date: 12/13/2024 10:30:45 AM
GetValueOrDefault: 1/1/0001 12:00:00 AM
GetValueOrDefault with custom: 1/1/2020 12:00:00 AM

Practical Database Scenario

using System;

public class Employee {
    public string Name { get; set; }
    public DateTime? HireDate { get; set; }
    public DateTime? TerminationDate { get; set; }
    
    public void DisplayInfo() {
        Console.WriteLine($"Employee: {Name}");
        Console.WriteLine($"Hire Date: {(HireDate.HasValue ? HireDate.Value.ToString("MM/dd/yyyy") : "Not specified")}");
        Console.WriteLine($"Termination Date: {(TerminationDate.HasValue ? TerminationDate.Value.ToString("MM/dd/yyyy") : "Still employed")}");
        Console.WriteLine();
    }
}

class Program {
    static void Main() {
        Employee emp1 = new Employee {
            Name = "John Smith",
            HireDate = new DateTime(2020, 5, 15),
            TerminationDate = null
        };
        
        Employee emp2 = new Employee {
            Name = "Jane Doe",
            HireDate = new DateTime(2019, 3, 10),
            TerminationDate = new DateTime(2023, 8, 20)
        };
        
        emp1.DisplayInfo();
        emp2.DisplayInfo();
    }
}

The output of the above code is −

Employee: John Smith
Hire Date: 05/15/2020
Termination Date: Still employed

Employee: Jane Doe
Hire Date: 03/10/2019
Termination Date: 08/20/2023

Null Coalescing with Nullable DateTime

using System;

class Program {
    static void Main() {
        DateTime? birthDate = null;
        DateTime? eventDate = new DateTime(2023, 12, 25);
        
        // Using null coalescing operator (??)
        DateTime defaultBirth = birthDate ?? DateTime.MinValue;
        DateTime defaultEvent = eventDate ?? DateTime.Now;
        
        Console.WriteLine("Birth Date: " + defaultBirth);
        Console.WriteLine("Event Date: " + defaultEvent);
        
        // Null conditional operator (?.)
        Console.WriteLine("Birth year: " + (birthDate?.Year ?? 0));
        Console.WriteLine("Event year: " + (eventDate?.Year ?? 0));
    }
}

The output of the above code is −

Birth Date: 1/1/0001 12:00:00 AM
Event Date: 12/25/2023 12:00:00 AM
Birth year: 0
Event year: 2023

Conclusion

Nullable DateTime in C# provides a safe way to handle date values that might be null, commonly used in database scenarios and optional parameters. Always check HasValue before accessing Value, or use GetValueOrDefault() for safe access with fallback values.

Updated on: 2026-03-17T07:04:35+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements