DateTimeOffset.AddTicks() Method in C#


The DateTimeOffset.AddTicks() method in C# is used to add a specified number of ticks to the value of this instance.

Syntax

Following is the syntax −

public DateTimeOffset AddTicks (long val);

Above, the Val is the ticks, which is a number of 100-nanosecond ticks. To subtract ticks, set a negative value.

Let us see the number of ticks value −

Time intervalNumber of ticks
Second10,000,000
Minute600,000,000
Hour36,000,000,000
Day864,000,000,000
Week6,048,000,000,000
MonthIt depends on the number of days in the month.
Non-leap year315,360,000,000,000
Leap year316,224,000,000,000

Example

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

using System;
public class Demo {
   public static void Main() {
      DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 11, 11, 8, 20, 10, new TimeSpan(-5, 0, 0));
      Console.WriteLine("DateTimeOffset (before adding ticks) = {0}", dateTimeOffset);
      // 3 seconds = 30,000,000 ticks
      DateTimeOffset res = dateTimeOffset.AddTicks(30000000);
      Console.WriteLine("DateTimeOffset (after adding ticks) = {0}", res);
   }
}

Output

This will produce the following output −

DateTimeOffset (before adding ticks) = 11/11/2019 8:20:10 AM -05:00
DateTimeOffset (after adding ticks) = 11/11/2019 8:20:13 AM -05:00

Example

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

using System;
public class Demo {
   public static void Main() {
      DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 11, 11, 8, 20, 10, new TimeSpan(-5, 0, 0));
      Console.WriteLine("DateTimeOffset (before subtracting ticks) = {0}", dateTimeOffset);
      // 2 seconds = 20,000,000 ticks
      DateTimeOffset res = dateTimeOffset.AddTicks(-20000000);
      Console.WriteLine("DateTimeOffset (after subtracting ticks) = {0}", res);
   }
}

Output

This will produce the following output −

DateTimeOffset (before subtracting ticks) = 11/11/2019 8:20:10 AM -05:00
DateTimeOffset (after subtracting ticks) = 11/11/2019 8:20:08 AM -05:00

Updated on: 12-Nov-2019

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements