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
DateTime.AddTicks() Method in C#
The DateTime.AddTicks() method in C# is used to add a specified number of ticks to the value of this instance. It returns a new DateTime representing the adjusted time. A tick represents 100 nanoseconds, making this method useful for precise time calculations.
Syntax
Following is the syntax −
public DateTime AddTicks(long ticks);
Parameters
ticks: A long value representing the number of ticks to add. Each tick equals 100 nanoseconds. Positive values add time, while negative values subtract time.
Return Value
Returns a new DateTime object whose value is the sum of the date and time represented by this instance and the time represented by ticks.
Using AddTicks() with Positive Values
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 07, 25, 6, 20, 40);
DateTime d2 = d1.AddTicks(5000);
Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("Initial Ticks = {0}", d1.Ticks);
Console.WriteLine("New Ticks = {0}", d2.Ticks);
Console.WriteLine("Difference = {0} ticks", d2.Ticks - d1.Ticks);
}
}
The output of the above code is −
DateTime = 25 July 2019, 06:20:40 Initial Ticks = 636996324400000000 New Ticks = 636996324400005000 Difference = 5000 ticks
Using AddTicks() with Negative Values
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 07, 25, 6, 20, 40);
DateTime d2 = d1.AddTicks(-5000);
Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("Initial Ticks = {0}", d1.Ticks);
Console.WriteLine("New Ticks (subtracting) = {0}", d2.Ticks);
Console.WriteLine("Time moved backward by: {0} ticks", d1.Ticks - d2.Ticks);
}
}
The output of the above code is −
DateTime = 25 July 2019, 06:20:40 Initial Ticks = 636996324400000000 New Ticks (subtracting) = 636996324399995000 Time moved backward by: 5000 ticks
Practical Example with Millisecond Conversion
Example
using System;
public class Demo {
public static void Main() {
DateTime now = DateTime.Now;
Console.WriteLine("Current time: {0:HH:mm:ss.fff}", now);
// Add 1 millisecond (10,000 ticks)
DateTime future = now.AddTicks(10000);
Console.WriteLine("After adding 1ms: {0:HH:mm:ss.fff}", future);
// Add 1 second (10,000,000 ticks)
DateTime oneSecondLater = now.AddTicks(10000000);
Console.WriteLine("After adding 1s: {0:HH:mm:ss.fff}", oneSecondLater);
Console.WriteLine("\nTick values:");
Console.WriteLine("1 millisecond = {0} ticks", TimeSpan.TicksPerMillisecond);
Console.WriteLine("1 second = {0} ticks", TimeSpan.TicksPerSecond);
}
}
The output of the above code is −
Current time: 14:30:25.123 After adding 1ms: 14:30:25.124 After adding 1s: 14:30:26.123 Tick values: 1 millisecond = 10000 ticks 1 second = 10000000 ticks
Common Use Cases
High-precision timing: When working with performance measurements or scientific calculations requiring sub-millisecond precision.
Time synchronization: Adjusting timestamps with precise increments for system synchronization.
Database operations: Converting between different time representations that use tick-based storage.
Conclusion
The DateTime.AddTicks() method provides the highest precision for DateTime arithmetic in C#, allowing manipulation at the 100-nanosecond level. It's essential for applications requiring precise time calculations, with positive values adding time and negative values subtracting time from the original DateTime instance.
