DateTimeOffset.Add() Method in C#

The DateTimeOffset.Add() method in C# returns a new DateTimeOffset object that represents the original date and time with a specified time interval added to it. This method is useful for performing date and time arithmetic while preserving the original offset information.

Syntax

Following is the syntax −

public DateTimeOffset Add(TimeSpan timespan);

Parameters

  • timespan − A TimeSpan object that represents the time interval to add. This can be positive (to add time) or negative (to subtract time).

Return Value

Returns a new DateTimeOffset object whose value is the sum of the date and time represented by the current DateTimeOffset instance and the time interval represented by timespan.

DateTimeOffset.Add() Operation Original DateTimeOffset + TimeSpan = New Result Offset information is preserved

Using Add() to Add Hours

Example

using System;

public class Demo {
   public static void Main() {
      DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 10, 10, 9, 45, 20, new TimeSpan(-10, 0, 0));
      TimeSpan ts = new TimeSpan(20, 0, 0);
      DateTimeOffset res = dateTimeOffset.Add(ts);
      Console.WriteLine("Original DateTimeOffset = {0}", dateTimeOffset);
      Console.WriteLine("Added TimeSpan = {0}", ts);
      Console.WriteLine("Result DateTimeOffset = {0}", res);
   }
}

The output of the above code is −

Original DateTimeOffset = 10/10/2019 9:45:20 AM -10:00
Added TimeSpan = 20:00:00
Result DateTimeOffset = 10/11/2019 5:45:20 AM -10:00

Using Add() with Minimum Value

Example

using System;

public class Demo {
   public static void Main() {
      DateTimeOffset dateTimeOffset = DateTimeOffset.MinValue;
      TimeSpan ts = new TimeSpan(20, 0, 0);
      DateTimeOffset res = dateTimeOffset.Add(ts);
      Console.WriteLine("Minimum DateTimeOffset = {0}", dateTimeOffset);
      Console.WriteLine("After adding 20 hours = {0}", res);
   }
}

The output of the above code is −

Minimum DateTimeOffset = 1/1/0001 12:00:00 AM +00:00
After adding 20 hours = 1/1/0001 8:00:00 PM +00:00

Using Add() with Negative TimeSpan

Example

using System;

public class Demo {
   public static void Main() {
      DateTimeOffset currentDate = new DateTimeOffset(2023, 6, 15, 14, 30, 0, TimeSpan.Zero);
      TimeSpan negativeSpan = new TimeSpan(-2, -30, -45); // Subtract 2 hours, 30 minutes, 45 seconds
      DateTimeOffset result = currentDate.Add(negativeSpan);
      Console.WriteLine("Original Date: {0}", currentDate);
      Console.WriteLine("Subtracting: {0}", negativeSpan);
      Console.WriteLine("Result: {0}", result);
   }
}

The output of the above code is −

Original Date: 6/15/2023 2:30:00 PM +00:00
Subtracting: -02:30:45
Result: 6/15/2023 11:59:15 AM +00:00

Conclusion

The DateTimeOffset.Add() method provides a reliable way to perform date and time arithmetic while preserving timezone offset information. It accepts both positive and negative TimeSpan values, allowing you to add or subtract time intervals from any DateTimeOffset instance.

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

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements