Date Class in C#

The DateTime class in C# is used to work with dates and times. It represents an instant in time, ranging from 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D. The DateTime class provides various methods and properties to create, manipulate, and format date and time values.

Syntax

Following is the syntax for creating a DateTime object −

DateTime variableName = new DateTime(year, month, day);
DateTime variableName = new DateTime(year, month, day, hour, minute, second);

To get the current date and time −

DateTime current = DateTime.Now;

Creating DateTime Objects

You can create DateTime objects by specifying year, month, and day values. You can also include hour, minute, and second components −

using System;

class Test {
    static void Main() {
        DateTime dt = new DateTime(2018, 7, 24);
        Console.WriteLine(dt.ToString());
        
        DateTime dtWithTime = new DateTime(2018, 7, 24, 14, 30, 45);
        Console.WriteLine(dtWithTime.ToString());
    }
}

The output of the above code is −

7/24/2018 12:00:00 AM
7/24/2018 2:30:45 PM

Getting Current Date and Time

The DateTime.Now property returns the current date and time of the system −

using System;

class Test {
    static void Main() {
        Console.WriteLine("Current Date and Time: " + DateTime.Now.ToString());
        Console.WriteLine("Current Date Only: " + DateTime.Today.ToString());
        Console.WriteLine("UTC Time: " + DateTime.UtcNow.ToString());
    }
}

The output of the above code is −

Current Date and Time: 9/17/2018 5:49:21 AM
Current Date Only: 9/17/2018 12:00:00 AM
UTC Time: 9/17/2018 12:49:21 PM

Adding Time to DateTime

DateTime provides several methods to add time intervals like days, hours, minutes, and seconds −

using System;

class Test {
    static void Main() {
        DateTime dt1 = new DateTime(2018, 7, 23, 08, 20, 10);
        Console.WriteLine("Original Date: " + dt1.ToString());
        
        DateTime dt2 = dt1.AddDays(7);
        Console.WriteLine("After adding 7 days: " + dt2.ToString());
        
        DateTime dt3 = dt1.AddHours(5);
        Console.WriteLine("After adding 5 hours: " + dt3.ToString());
        
        DateTime dt4 = dt1.AddMonths(2);
        Console.WriteLine("After adding 2 months: " + dt4.ToString());
    }
}

The output of the above code is −

Original Date: 7/23/2018 8:20:10 AM
After adding 7 days: 7/30/2018 8:20:10 AM
After adding 5 hours: 7/23/2018 1:20:10 PM
After adding 2 months: 9/23/2018 8:20:10 AM

Formatting DateTime

You can format DateTime objects using various format strings −

using System;

class Test {
    static void Main() {
        DateTime dt = new DateTime(2018, 7, 24, 14, 30, 45);
        
        Console.WriteLine("Default: " + dt.ToString());
        Console.WriteLine("Short Date: " + dt.ToString("d"));
        Console.WriteLine("Long Date: " + dt.ToString("D"));
        Console.WriteLine("Time: " + dt.ToString("T"));
        Console.WriteLine("Custom: " + dt.ToString("yyyy-MM-dd HH:mm:ss"));
    }
}

The output of the above code is −

Default: 7/24/2018 2:30:45 PM
Short Date: 7/24/2018
Long Date: Tuesday, July 24, 2018
Time: 2:30:45 PM
Custom: 2018-07-24 14:30:45

Common DateTime Properties

Property Description
Year Gets the year component (1 to 9999)
Month Gets the month component (1 to 12)
Day Gets the day component (1 to 31)
Hour Gets the hour component (0 to 23)
Minute Gets the minute component (0 to 59)
Second Gets the second component (0 to 59)

Conclusion

The DateTime class in C# is essential for handling date and time operations. It provides comprehensive methods for creating, manipulating, and formatting dates and times, making it easy to work with temporal data in applications.

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

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements