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.AddMilliseconds() Method in C#
The DateTime.AddMilliseconds() method in C# is used to add a specified number of milliseconds to a DateTime instance. This method returns a new DateTime object with the updated value, leaving the original DateTime unchanged.
Syntax
Following is the syntax −
public DateTime AddMilliseconds(double value);
Parameters
-
value − A double representing the number of milliseconds to add. Can be positive or negative.
Return Value
Returns a new DateTime object whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by value.
Adding Positive Milliseconds
When adding positive milliseconds, the method increases the time forward −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 11, 10, 5, 0, 25);
DateTime d2 = d1.AddMilliseconds(1000);
Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("New DateTime (After adding 1000 milliseconds) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d2);
}
}
The output of the above code is −
Initial DateTime = 10 November 2019, 05:00:25 New DateTime (After adding 1000 milliseconds) = 10 November 2019, 05:00:26
Adding Negative Milliseconds
When adding negative milliseconds, the method effectively subtracts time, moving backward −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 10, 5, 9, 0, 10);
DateTime d2 = d1.AddMilliseconds(-2000);
Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("New DateTime (After subtracting 2000 milliseconds) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d2);
}
}
The output of the above code is −
Initial DateTime = 05 October 2019, 09:00:10 New DateTime (After subtracting 2000 milliseconds) = 05 October 2019, 09:00:08
Working with Fractional Milliseconds
The method accepts double values, allowing for fractional milliseconds −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2023, 5, 15, 10, 30, 45, 500);
DateTime d2 = d1.AddMilliseconds(250.75);
Console.WriteLine("Initial DateTime = {0:yyyy-MM-dd HH:mm:ss.fff}", d1);
Console.WriteLine("New DateTime (After adding 250.75 milliseconds) = {0:yyyy-MM-dd HH:mm:ss.fff}", d2);
Console.WriteLine("Difference in ticks: {0}", d2.Ticks - d1.Ticks);
}
}
The output of the above code is −
Initial DateTime = 2023-05-15 10:30:45.500 New DateTime (After adding 250.75 milliseconds) = 2023-05-15 10:30:45.751 Difference in ticks: 2507500
Conclusion
The DateTime.AddMilliseconds() method provides precise time manipulation at the millisecond level. It accepts both positive and negative values, allowing you to add or subtract time, and works with fractional milliseconds for high-precision scenarios.
