Skip to main content

How to Add Seconds and Milliseconds to a DateTime in C#

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

start.AddSeconds(12.5) gives the time 12.5 seconds after start, and AddMilliseconds(250) adds a quarter of a second. Both take fractions and negative numbers, and both hand you a new DateTime instead of changing the one you call them on.

A DateTime holds a date and a time of day. AddSeconds and AddMilliseconds are two of its methods for moving that time forward or back. Microsoft's API reference describes them this way: Returns a new DateTime that adds the specified number of seconds to the value of this instance. And: Returns a new DateTime that adds the specified number of milliseconds to the value of this instance.

The word "new" matters. The reference adds: This method does not change the value of this DateTime. Instead, it returns a new DateTime whose value is the result of this operation.

Syntax​

Syntax
DateTime <later> = <time>.AddSeconds(<seconds>);
DateTime <later> = <time>.AddMilliseconds(<milliseconds>);
PartWhat it is
<time>The DateTime you start from. It stays as it is
AddSecondsAdds a number of seconds
<seconds>A double: whole and fractional seconds, such as 12.5. A negative number goes back in time
AddMillisecondsAdds a number of milliseconds, the thousandths of a second
<milliseconds>A double: whole and fractional milliseconds, such as 250
<later>The new DateTime that the method returns

The Example​

A console app called AddSecondsMilliseconds times a race. The race starts at exactly 10:00 UTC. The first runner finishes 12.5 seconds later, and the second runner 250 milliseconds after the first. There's also a ready signal 3 seconds before the start. The app prints every time down to the millisecond.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n AddSecondsMilliseconds
cd AddSecondsMilliseconds
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/AddSecondsMilliseconds/AddSecondsMilliseconds.csproj:
Determining projects to restore...
Restored /Users/you/source/AddSecondsMilliseconds/AddSecondsMilliseconds.csproj (in 48 ms).
Restore succeeded.

dotnet new console -n AddSecondsMilliseconds creates a folder named AddSecondsMilliseconds with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or AddSecondsMilliseconds.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the start time:

Program.cs (the start)
// A race starts at exactly 10:00 UTC. HH:mm:ss.fff shows the time down to the millisecond.
DateTime start = new DateTime(2026, 12, 3, 10, 0, 0, DateTimeKind.Utc);
Console.WriteLine($"Start: {start:HH:mm:ss.fff}");

new DateTime(2026, 12, 3, 10, 0, 0, DateTimeKind.Utc) is 10:00:00 on 3 December 2026, marked as a UTC time. In {start:HH:mm:ss.fff}, everything after start: is a format: hours, minutes, seconds and, after the dot, fff. Microsoft's format reference: The "fff" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.

Then AddSeconds with a fraction:

Program.cs (AddSeconds)
// AddSeconds takes whole and fractional seconds: 12.5 is 12 seconds and 500 milliseconds.
DateTime first = start.AddSeconds(12.5);
Console.WriteLine($"First runner: {first:HH:mm:ss.fff}");

start.AddSeconds(12.5) returns the time 12.5 seconds after start, and first keeps it. The reference explains the fraction: The fractional part of value is the fractional part of a second. For example, 4.5 is equivalent to 4 seconds, 500 milliseconds, and 0 ticks.

Then AddMilliseconds:

Program.cs (AddMilliseconds)
// AddMilliseconds adds thousandths of a second: the second runner is 250 ms behind.
DateTime second = first.AddMilliseconds(250);
Console.WriteLine($"Second runner: {second:HH:mm:ss.fff}");

This call starts from first, the first runner's time, and adds 250 milliseconds to it.

Then a negative number:

Program.cs (going back)
// A negative number goes back in time.
DateTime ready = start.AddSeconds(-3);
Console.WriteLine($"Ready signal: {ready:HH:mm:ss.fff}");

AddSeconds(-3) gives the time 3 seconds before the start. The reference: The value parameter can be negative or positive.

Last, start once more:

Program.cs (start is unchanged)
// Each call returns a new DateTime, so start itself hasn't changed.
Console.WriteLine($"Start is still: {start:HH:mm:ss.fff}");
Console.WriteLine($"Second runner's time: {(second - start).TotalSeconds} seconds");

None of the calls changed start. second - start is the time between the start and the second runner, and TotalSeconds gives it as a number of seconds.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Start: 10:00:00.000
First runner: 10:00:12.500
Second runner: 10:00:12.750
Ready signal: 09:59:57.000
Start is still: 10:00:00.000
Second runner's time: 12.75 seconds

macOS-style terminal window titled Terminal — AddSecondsMilliseconds showing dotnet run printing the start at 10:00:00.000, the first runner at 10:00:12.500, the second runner at 10:00:12.750, the ready signal at 09:59:57.000, the start still at 10:00:00.000 and the second runner's time as 12.75 seconds

The first runner finished at 10:00:12.500, 12.5 seconds after the start, and the second runner at 10:00:12.750, 250 milliseconds later. The ready signal printed 09:59:57.000, 3 seconds before 10:00. After all those calls, start still printed 10:00:00.000, because each method returned a new DateTime. The second runner's time came to 12.75 seconds.

The Complete File​

macOS-style code window titled Program.cs — AddSecondsMilliseconds showing a race start at 10:00 UTC, AddSeconds(12.5) for the first runner, AddMilliseconds(250) for the second runner, AddSeconds(-3) for a ready signal, and the times printed with the HH:mm.fff format

Program.cs
// A race starts at exactly 10:00 UTC. HH:mm:ss.fff shows the time down to the millisecond.
DateTime start = new DateTime(2026, 12, 3, 10, 0, 0, DateTimeKind.Utc);
Console.WriteLine($"Start: {start:HH:mm:ss.fff}");

// AddSeconds takes whole and fractional seconds: 12.5 is 12 seconds and 500 milliseconds.
DateTime first = start.AddSeconds(12.5);
Console.WriteLine($"First runner: {first:HH:mm:ss.fff}");

// AddMilliseconds adds thousandths of a second: the second runner is 250 ms behind.
DateTime second = first.AddMilliseconds(250);
Console.WriteLine($"Second runner: {second:HH:mm:ss.fff}");

// A negative number goes back in time.
DateTime ready = start.AddSeconds(-3);
Console.WriteLine($"Ready signal: {ready:HH:mm:ss.fff}");

// Each call returns a new DateTime, so start itself hasn't changed.
Console.WriteLine($"Start is still: {start:HH:mm:ss.fff}");
Console.WriteLine($"Second runner's time: {(second - start).TotalSeconds} seconds");

Common Mistakes​

MistakeWhat happensDo this instead
Passing milliseconds to AddSecondsstart.AddSeconds(500) printed 10:08:20.000, more than 8 minutes after the start instead of half a secondUse AddMilliseconds(500), which printed 10:00:00.500
Turning milliseconds into seconds with delay / 1000 when delay is an intWith delay set to 1500, delay / 1000 was 1, so AddSeconds(delay / 1000) printed 10:00:01.000 and lost half a secondUse AddMilliseconds(delay), or divide by 1000.0: both printed 10:00:01.500
Calling start.AddSeconds(30); on its own linestart still printed 10:00:00.000, and the build gave no warningKeep the result: after start = start.AddSeconds(30);, start printed 10:00:30.000
Printing with HH:mm:ss10:00:12.500 and 10:00:12.750 both printed as 10:00:12, although first == second was FalseAdd .fff to see the milliseconds

FAQ​

Does AddMilliseconds keep a fraction of a millisecond?​

Yes, on .NET 10. AddMilliseconds(1.5) printed 10:00:00.0015000 with the seven-digit format fffffff, and the time moved by 15000 ticks. The API reference: A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, and: In .NET 7 and later versions, the full Double precision of the value parameter is used. However, due to the inherent imprecision of floating point math, the resulting precision will vary. The same page's note on the value parameter still says that the value is rounded to the nearest integer. I reported the mismatch to the .NET API docs as issue #13410.

How do I add microseconds?​

With AddMicroseconds, which .NET 7 added. The reference: Returns a new DateTime that adds the specified number of microseconds to the value of this instance. start.AddMicroseconds(250) printed 10:00:00.000250 with the six-digit format ffffff.

What if the result is too far in the past or the future?​

You get an ArgumentOutOfRangeException. DateTime.MaxValue.AddSeconds(1) threw one with the message The added or subtracted value results in an un-representable DateTime. (Parameter 'value'). The reference lists it for this case: The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.

Conclusion​

AddSeconds and AddMilliseconds move a DateTime by seconds or milliseconds: forward with a positive number, back with a negative one, and with fractions such as 12.5. They return a new DateTime, so keep the result in a variable, and print with .fff when the milliseconds matter.

Additional Resources​