DateTimeOffset.Add() Method in C#


The DateTimeOffset.Add() method in C# is used to return a new DateTimeOffset object that adds a specified time interval to the value of this instance.

Syntax

Following is the syntax −

public DateTimeOffset Add (TimeSpan t);

Example

Let us now see an example to implement the DateTimeOffset.Add() method −

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("DateTimeOffset = {0}", res);
   }
}

Output

This will produce the following output −

DateTimeOffset = 10/11/2019 5:45:20 AM -10:00

Example

Let us now see another example to implement the DateTimeOffset.Add() method −

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("DateTimeOffset = {0}", res);
   }
}

Output

This will produce the following output −

DateTimeOffset = 1/1/0001 8:00:00 PM +00:00

Updated on: 11-Nov-2019

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements