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.AddTicks() Method in C#
The DateTimeOffset.AddTicks() method in C# is used to add a specified number of ticks to the value of a DateTimeOffset instance. A tick represents 100 nanoseconds, making it the smallest unit of time measurement in .NET.
Syntax
Following is the syntax −
public DateTimeOffset AddTicks(long ticks);
Parameters
ticks: A signed 64-bit integer representing the number of 100-nanosecond ticks to add. To subtract ticks, use a negative value.
Return Value
Returns a new DateTimeOffset instance with the specified ticks added to the original value. The original instance remains unchanged.
Understanding Tick Values
The following table shows the number of ticks for common time intervals −
| Time Interval | Number of Ticks |
|---|---|
| Second | 10,000,000 |
| Minute | 600,000,000 |
| Hour | 36,000,000,000 |
| Day | 864,000,000,000 |
| Week | 6,048,000,000,000 |
| Non-leap year | 315,360,000,000,000 |
| Leap year | 316,224,000,000,000 |
Adding Ticks Example
The following example demonstrates adding ticks to a DateTimeOffset −
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);
}
}
The output of the above code is −
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
Subtracting Ticks Example
The following example demonstrates subtracting ticks using a negative value −
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 (negative to subtract)
DateTimeOffset res = dateTimeOffset.AddTicks(-20000000);
Console.WriteLine("DateTimeOffset (after subtracting ticks) = {0}", res);
}
}
The output of the above code is −
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
Precision Timing Example
The following example shows how to add precise microsecond intervals using ticks −
using System;
public class Demo {
public static void Main() {
DateTimeOffset start = new DateTimeOffset(2023, 12, 25, 14, 30, 45, new TimeSpan(0, 0, 0));
Console.WriteLine("Start time: {0}", start);
// Add 500 microseconds (1 microsecond = 10 ticks)
DateTimeOffset withMicroseconds = start.AddTicks(5000);
Console.WriteLine("After adding 500 microseconds: {0}", withMicroseconds);
// Add 1.5 milliseconds (15,000 ticks)
DateTimeOffset withMilliseconds = start.AddTicks(15000);
Console.WriteLine("After adding 1.5 milliseconds: {0}", withMilliseconds);
}
}
The output of the above code is −
Start time: 12/25/2023 2:30:45 PM +00:00 After adding 500 microseconds: 12/25/2023 2:30:45 PM +00:00 After adding 1.5 milliseconds: 12/25/2023 2:30:45 PM +00:00
Conclusion
The DateTimeOffset.AddTicks() method provides precise time manipulation at the tick level, where each tick represents 100 nanoseconds. This method is essential for high-precision timing operations and returns a new instance while preserving the original DateTimeOffset value.
