DateTime.AddHours() Method in C#

The DateTime.AddHours() method in C# is used to add the specified number of hours to the value of this instance. This method returns a new DateTime object, leaving the original instance unchanged.

Syntax

Following is the syntax −

public DateTime AddHours(double hrs);

Parameters

hrs − A number of hours to be added. The value can be positive (to add hours) or negative (to subtract hours). Fractional values represent parts of an hour.

Return Value

Returns a new DateTime object that represents the date and time that results from adding the specified number of hours to the current instance.

DateTime.AddHours() Operation Original DateTime 09:00 AM + 2 hours New DateTime 11:00 AM Original DateTime remains unchanged

Using AddHours() to Add Hours

Example

using System;

public class Demo {
    public static void Main() {
        DateTime d1 = new DateTime(2019, 11, 2, 9, 0, 10);
        DateTime d2 = d1.AddHours(2);
        Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:t}", d1);
        Console.WriteLine("New DateTime (After adding hours) = {0:dd} {0:y}, {0:t}", d2);
    }
}

The output of the above code is −

Initial DateTime = 02 November 2019, 9:00 AM
New DateTime (After adding hours) = 02 November 2019, 11:00 AM

Using AddHours() to Subtract Hours

Example

using System;

public class Demo {
    public static void Main() {
        DateTime d1 = new DateTime(2019, 10, 5, 9, 0, 10);
        DateTime d2 = d1.AddHours(-5);
        Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:t}", d1);
        Console.WriteLine("New DateTime (After subtracting hours) = {0:dd} {0:y}, {0:t}", d2);
    }
}

The output of the above code is −

Initial DateTime = 05 October 2019, 9:00 AM
New DateTime (After subtracting hours) = 05 October 2019, 4:00 AM

Using Fractional Hours

Example

using System;

public class Demo {
    public static void Main() {
        DateTime d1 = new DateTime(2023, 8, 15, 14, 30, 0);
        DateTime d2 = d1.AddHours(2.5);  // Add 2 hours and 30 minutes
        DateTime d3 = d1.AddHours(-1.25); // Subtract 1 hour and 15 minutes
        Console.WriteLine("Initial DateTime: {0:yyyy-MM-dd HH:mm:ss}", d1);
        Console.WriteLine("After adding 2.5 hours: {0:yyyy-MM-dd HH:mm:ss}", d2);
        Console.WriteLine("After subtracting 1.25 hours: {0:yyyy-MM-dd HH:mm:ss}", d3);
    }
}

The output of the above code is −

Initial DateTime: 2023-08-15 14:30:00
After adding 2.5 hours: 2023-08-15 17:00:00
After subtracting 1.25 hours: 2023-08-15 13:15:00

Conclusion

The DateTime.AddHours() method provides a simple way to manipulate time by adding or subtracting hours from a DateTime instance. It supports both positive and negative values, including fractional hours, and returns a new DateTime object without modifying the original.

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

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements