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
Selected Reading
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 this instance.
Syntax
Following is the syntax −
public DateTimeOffset AddHours (double hrs);
Above, hrs are the hours to be added. To subtract hours, include a negative value.
Example
Let us now see an example to implement the DateTimeOffset.AddHours() method −
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);
}
}
Output
This will produce the following output −
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
Example
Let us now see another example to implement the DateTimeOffset.AddHours() method −
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);
}
}
Output
This will produce the following output −
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
Advertisements
