Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTimeOffset.AddHours() Method in C#
The DateTimeOffset.AddHours() method in C# is used to add a specified number of whole and fractional hours to the value of a DateTimeOffset instance. This method returns a new DateTimeOffset object that represents the original date and time plus the specified hours, while preserving the original offset from UTC.
Syntax
Following is the syntax −
public DateTimeOffset AddHours(double hours);
Parameters
hours − A double value representing the number of hours to add. This can be a whole number or fractional value. To subtract hours, provide a negative value.
Return Value
Returns a new DateTimeOffset object whose value is the sum of the date and time represented by the current instance and the number of hours specified by the hours parameter.
Using AddHours() to Add Hours
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 10, 10, 3, 15, 0, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset (before adding hours) = {0}", dateTimeOffset);
DateTimeOffset res = dateTimeOffset.AddHours(4);
Console.WriteLine("DateTimeOffset (after adding hours) = {0}", res);
}
}
The output of the above code is −
DateTimeOffset (before adding hours) = 10/10/2019 3:15:00 AM -05:00 DateTimeOffset (after adding hours) = 10/10/2019 7:15:00 AM -05:00
Using AddHours() to Subtract Hours
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 11, 10, 3, 15, 0, new TimeSpan(-5, 0, 0));
Console.WriteLine("DateTimeOffset (before subtracting hours) = {0}", dateTimeOffset);
DateTimeOffset res = dateTimeOffset.AddHours(-2);
Console.WriteLine("DateTimeOffset (after subtracting hours) = {0}", res);
}
}
The output of the above code is −
DateTimeOffset (before subtracting hours) = 11/10/2019 3:15:00 AM -05:00 DateTimeOffset (after subtracting hours) = 11/10/2019 1:15:00 AM -05:00
Using Fractional Hours
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 12, 15, 10, 30, 0, new TimeSpan(2, 0, 0));
Console.WriteLine("Original DateTimeOffset = {0}", dateTimeOffset);
DateTimeOffset result1 = dateTimeOffset.AddHours(2.5);
Console.WriteLine("After adding 2.5 hours = {0}", result1);
DateTimeOffset result2 = dateTimeOffset.AddHours(0.25);
Console.WriteLine("After adding 0.25 hours (15 minutes) = {0}", result2);
}
}
The output of the above code is −
Original DateTimeOffset = 12/15/2019 10:30:00 AM +02:00 After adding 2.5 hours = 12/15/2019 1:00:00 PM +02:00 After adding 0.25 hours (15 minutes) = 12/15/2019 10:45:00 AM +02:00
Conclusion
The DateTimeOffset.AddHours() method provides a convenient way to add or subtract hours from a DateTimeOffset instance while preserving the original UTC offset. It accepts both whole and fractional hour values, making it flexible for various time calculation scenarios.
