C# DateTime AddMilliseconds() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

In C#, DateTime.AddMilliseconds shifts a value by thousandths of a second. It is useful for timeouts, animations, and log timestamps.

One millisecond equals 10,000 ticks, and one tick is the smallest step a DateTime stores. Print milliseconds with the fff format.

This is an instance member of System.DateTime. You call it on a DateTime value, such as dt.AddMilliseconds(...). DateTime is a struct, so every value is copied and never changed in place.

Syntax

Pass the number of milliseconds to add.

C#
DateTime t = new DateTime(2026, 11, 18, 14, 30, 45);
DateTime a = t.AddMilliseconds(250);    // 14:30:45.250
DateTime b = t.AddMilliseconds(1500);   // 14:30:46.500
DateTime c = t.AddMilliseconds(-500);   // 14:30:44.500
CallWhat it does
t.AddMilliseconds(250)A quarter of a second later.
t.AddMilliseconds(1500)One and a half seconds later (the seconds value changes).
t.AddMilliseconds(-500)Half a second earlier.

Format milliseconds with ToString("HH:mm:ss.fff"). Newer .NET versions keep fractions of a millisecond; older ones round to the nearest whole millisecond.

Library declarations
C#
public DateTime AddMilliseconds(double value);

Parameters

ParameterDescription
valueRequired. Number of milliseconds to add. Negative values go backward.

Return Value

Return ValueDescription
DateTimeA new DateTime shifted by value milliseconds, with the same Kind.

C# Examples

Example 1 — Add 250 and 1500 ms

Add milliseconds and print them with the fff pattern.

C#
using System;
using System.Globalization;

class Program {
    static string Clock(DateTime d) => d.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture);

    static void Main() {
        DateTime t = new DateTime(2026, 11, 18, 14, 30, 45);
        Console.WriteLine(Clock(t.AddMilliseconds(250)));
        Console.WriteLine(Clock(t.AddMilliseconds(1500)));
    }
}

How It Works

  1. Start at a whole second. t is 14:30:45 with no milliseconds.
  2. Add 250. The milliseconds field becomes .250 and the second stays 45.
  3. Add 1500. One full second carries over, so you get 46 seconds and .500.
  4. fff pattern. It always prints three digits, even when the value is zero.

Example 2 — Milliseconds Are Ticks

Measure how many ticks one millisecond adds.

C#
using System;

class Program {
    static void Main() {
        DateTime before = new DateTime(2026, 11, 18, 14, 30, 45);
        DateTime after = before.AddMilliseconds(1);
        Console.WriteLine(after.Ticks - before.Ticks);
        Console.WriteLine(TimeSpan.TicksPerMillisecond);
    }
}

How It Works

  1. Two values. before and after differ by exactly one millisecond.
  2. Ticks property. Ticks is a long count of 100-nanosecond steps.
  3. Difference is 10000. One millisecond equals 10,000 ticks.
  4. Constant check. TimeSpan.TicksPerMillisecond gives the same number, so you never hard-code it.

Example 3 — Build a Timeout Deadline

Create a deadline 2.5 seconds after a start time and test two moments against it.

C#
using System;
using System.Globalization;

class Program {
    static string Clock(DateTime d) => d.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture);

    static void Main() {
        DateTime start = new DateTime(2026, 11, 18, 12, 0, 0);
        DateTime deadline = start.AddMilliseconds(2500);
        DateTime a = start.AddMilliseconds(1200);
        DateTime b = start.AddMilliseconds(3000);
        Console.WriteLine(Clock(deadline));
        Console.WriteLine(a < deadline);
        Console.WriteLine(b < deadline);
    }
}

How It Works

  1. Set a deadline. Start at noon and allow 2500 milliseconds.
  2. Deadline text. The value prints as 12:00:02.500, two and a half seconds after noon.
  3. Early moment. 1200 ms is before the deadline, so the comparison prints True.
  4. Late moment. 3000 ms is after the deadline, so the comparison prints False.

Frequently Asked Questions

There are 10,000 ticks in one millisecond (TimeSpan.TicksPerMillisecond).
The default ToString hides them. Use a format such as HH:mm:ss.fff.
Yes. A negative value moves the time earlier.
Older .NET versions round to the nearest millisecond. Newer versions keep fractional milliseconds.
No. For benchmarks use Stopwatch. DateTime is better for calendar and clock values.

Next: AddMinutes()

Add or subtract minutes and get a new DateTime with System.DateTime.

AddMinutes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful