How can I exactly construct a time stamp of actual time with milliseconds precision?

I suspect you mean millisecond accuracy. DateTime has a lot of precision, but is fairly coarse in terms of accuracy. Generally speaking, you can't. Usually the system clock (which is where DateTime.Now gets its data from) has a resolution of around 10-15 ms. See Eric Lippert's blog post about precision and accuracy for more details.

If you need more accurate timing than this, you may want to look into using an NTP client.

However, it's not clear that you really need millisecond accuracy here. If you don't care about the exact timing - you just want to show the samples in the right order, with "pretty good" accuracy, then the system clock should be fine. I'd advise you to use DateTime.UtcNow rather than DateTime.Now though, to avoid time zone issues around daylight saving transitions, etc.

If your question is actually just around converting a DateTime to a string with millisecond precision, I'd suggest using:

string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff",
                                            CultureInfo.InvariantCulture);

(Note that unlike your sample, this is sortable and less likely to cause confusion around whether it's meant to be "month/day/year" or "day/month/year".)

Answer from Jon Skeet on Stack Overflow
Top answer
1 of 12
396

How can I exactly construct a time stamp of actual time with milliseconds precision?

I suspect you mean millisecond accuracy. DateTime has a lot of precision, but is fairly coarse in terms of accuracy. Generally speaking, you can't. Usually the system clock (which is where DateTime.Now gets its data from) has a resolution of around 10-15 ms. See Eric Lippert's blog post about precision and accuracy for more details.

If you need more accurate timing than this, you may want to look into using an NTP client.

However, it's not clear that you really need millisecond accuracy here. If you don't care about the exact timing - you just want to show the samples in the right order, with "pretty good" accuracy, then the system clock should be fine. I'd advise you to use DateTime.UtcNow rather than DateTime.Now though, to avoid time zone issues around daylight saving transitions, etc.

If your question is actually just around converting a DateTime to a string with millisecond precision, I'd suggest using:

string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff",
                                            CultureInfo.InvariantCulture);

(Note that unlike your sample, this is sortable and less likely to cause confusion around whether it's meant to be "month/day/year" or "day/month/year".)

2 of 12
151

This should work:

DateTime.Now.ToString("hh.mm.ss.ffffff");

If you don't need it to be displayed and just need to know the time difference, well don't convert it to a String. Just leave it as, DateTime.Now();

And use TimeSpan to know the difference between time intervals:

Example

DateTime start;
TimeSpan time;

start = DateTime.Now;

//Do something here

time = DateTime.Now - start;
label1.Text = String.Format("{0}.{1}", time.Seconds, time.Milliseconds.ToString().PadLeft(3, '0'));
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › base-types › custom-date-and-time-format-strings
Custom date and time format strings - .NET | Microsoft Learn
Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 018) Dim ci As CultureInfo = CultureInfo.InvariantCulture Console.WriteLine(date1.ToString("hh:mm:ss.f", ci)) ' Displays 07:27:15.0 Console.WriteLine(date1.ToString("hh:mm:ss.F", ci)) ' Displays 07:27:15 Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci)) ' Displays 07:27:15.01 Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci)) ' Displays 07:27:15.01 Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci)) ' Displays 07:27:15.018 Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci)) ' Displays 07:27:15.018 ... 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.
🌐
DZone
dzone.com › articles › display-datetime-milliseconds
Display DateTime Up To Milliseconds
May 25, 2007 - Format string "ffff" is responsible for displaying milliseconds. string myTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff"); Console.WriteLine(myTime);
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › base-types › how-to-display-milliseconds-in-date-and-time-values
How to: Display Milliseconds in Date and Time Values - .NET | Microsoft Learn
Fractional parts of a millisecond are truncated instead of rounded in the returned string. These format specifiers are used in the following example: DateTime dateValue = new DateTime(2008, 7, 16, 8, 32, 45, 180); Console.WriteLine($"{dateValue.ToString("s.f")} seconds"); Console.WriteLine($"{dateValue.ToString("s.ff")} seconds"); Console.WriteLine($"{dateValue.ToString("s.ffff")} seconds"); // The example displays the following output to the console: // 45.1 seconds // 45.18 seconds // 45.1800 seconds
🌐
Tales from the Evil Empire
weblogs.asp.net › jeffwids › timestamp-string-with-milliseconds-from-a-system-datetime
Jeff Widmer's Blog - Timestamp string with milliseconds from a System.DateTime
DateTime d = DateTime.Now; String s = d.ToString("yyyyMMdd-HHmmss.fff"); Will produce a string with the value of “20091018-232013.456” assuming the date/time was 10/18/2009 11:20:13.456PM at the time the code was executed. I use this all the time when I need to append a timestamp to a log, ...
Top answer
1 of 16
1313
DateTime.Now.ToString("yyyyMMddHHmmss"); // case sensitive
2 of 16
738

C# Examples by Jan Slama has great examples on this, check it out:

// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}",      dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}",      dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}",      dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",          dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",               dt);  // "5 05"            minute
String.Format("{0:s ss}",               dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}",      dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}",      dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",               dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",           dt);  // "-6 -06 -06:00"   time zone
// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}",           dt);  // "3/9/2008"
String.Format("{0:MM/dd/yyyy}",         dt);  // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}",   dt);  // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}",           dt);  // "03/09/08"
String.Format("{0:MM/dd/yyyy}",         dt);  // "03/09/2008"

Standard DateTime Formatting

String.Format("{0:t}", dt);  // "4:05 PM"                           ShortTime
String.Format("{0:d}", dt);  // "3/9/2008"                          ShortDate
String.Format("{0:T}", dt);  // "4:05:07 PM"                        LongTime
String.Format("{0:D}", dt);  // "Sunday, March 09, 2008"            LongDate
String.Format("{0:f}", dt);  // "Sunday, March 09, 2008 4:05 PM"    LongDate+ShortTime
String.Format("{0:F}", dt);  // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt);  // "3/9/2008 4:05 PM"                  ShortDate+ShortTime
String.Format("{0:G}", dt);  // "3/9/2008 4:05:07 PM"               ShortDate+LongTime
String.Format("{0:m}", dt);  // "March 09"                          MonthDay
String.Format("{0:y}", dt);  // "March, 2008"                       YearMonth
String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"     RFC1123
String.Format("{0:s}", dt);  // "2008-03-09T16:05:07"               SortableDateTime
String.Format("{0:u}", dt);  // "2008-03-09 16:05:07Z"              UniversalSortableDateTime
Specifier DateTimeFormatInfo property Pattern value (for en-US culture)
t ShortTimePattern h:mm tt
d ShortDatePattern M/d/yyyy
T LongTimePattern h:mm:ss tt
D LongDatePattern dddd, MMMM dd, yyyy
f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt
F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt
g (combination of d and t) M/d/yyyy h:mm tt
G (combination of d and T) M/d/yyyy h:mm:ss tt
m, M MonthDayPattern MMMM dd
y, Y YearMonthPattern MMMM, yyyy
r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
s SortableDateTi­mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
u UniversalSorta­bleDateTimePat­tern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)
(*) = culture independent

Using C# 6 string interpolation format, the above line would be written this way:

// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

$"{dt:y yy yyy yyyy}";  // "8 08 008 2008"   year
$"{dt:M MM MMM MMMM}";  // "3 03 Mar March"  month
$"{dt:d dd ddd dddd}";  // "9 09 Sun Sunday" day
$"{dt:h hh H HH}";      // "4 04 16 16"      hour 12/24
$"{dt:m mm}";           // "5 05"            minute
$"{dt:s ss}";           // "7 07"            second
$"{dt:f ff fff ffff}";  // "1 12 123 1230"   sec.fraction
$"{dt:F FF FFF FFFF}";  // "1 12 123 123"    without zeroes
$"{dt:t tt}";           // "P PM"            A.M. or P.M.
$"{dt:z zz zzz}";       // "-6 -06 -06:00"   time zone

// month/day numbers without/with leading zeroes
$"{dt:M/d/yyyy}";    // "3/9/2008"
$"{dt:MM/dd/yyyy}";  // "03/09/2008"

// day/month names
$"{dt:ddd, MMM d, yyyy}";    // "Sun, Mar 9, 2008"
$"{dt:dddd, MMMM d, yyyy}";  // "Sunday, March 9, 2008"

// two/four digit year
$"{dt:MM/dd/yy}";    // "03/09/08"
$"{dt:MM/dd/yyyy}";  // "03/09/2008"
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.datetime.tostring
DateTime.ToString Method (System) | Microsoft Learn
Converts the value of the current DateTime object to its equivalent string representation using the specified format and the formatting conventions of the current culture. public: System::String ^ ToString(System::String ^ format);
🌐
Dot Net Guide
dotnetguide.com › home › .net › c# datetime format: complete 2025 cheat sheet + 50+ examples
C# DateTime Format: Complete 2025 Cheat Sheet + 50+ Examples:Working with Date &Time format in C#|
April 29, 2025 - Please check below. y = year, m = minutes / M = months, d= date, h = 12 hour, H = 24 hour, s= seconds · Let’s have a look at below C# DateTime formats and their outputs. Here we see all the patterns of the C# DateTime, format, and outputs. // For storage/transmission, use invariant culture string dbFormat = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss...
Find elsewhere
🌐
Tutorial Teacher
tutorialsteacher.com › articles › datetime-formats-in-csharp
DateTime Formats in C#
Specify the format as a string parameter in the ToString() method to get the date string in the required format. The following example demonstrates getting the date and time string in different formats.
🌐
C# Corner
c-sharpcorner.com › blogs › date-and-time-format-in-c-sharp-programming1
DateTime Format In C#
October 4, 2023 - DateTime aDate = DateTime.Now; // Format Datetime in different formats and display them Console.WriteLine(aDate.ToString("MM/dd/yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(aDate.ToString("dddd, dd MMMM yyyy HH:mm:ss")); Console.WriteLine(aDate.ToString("MM/dd/yyyy HH:mm")); Console.WriteLine(aDate.ToString("MM/dd/yyyy hh:mm tt"));
🌐
FreeASPHosting.net
freeasphosting.net › date-time-format-in-c-sharp-datetime-formatting-c-sharp.html
Date Time Format In C#: Let’s Format Your Time
The common method used for this is ToString(), which can be used with various format specifiers to change the format of the date or time. For instance, to get the current date and time, you can use DateTime.Now and convert this DateTime to a string, using ToString() method, like this:
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-chash-datetime-to-yyyymmddhhmmss-format
How to convert C# DateTime to “YYYYMMDDHHMMSS” format?
Convert the dateTime to toString that results in converting the DateTime to “YYYYMMDDHHMMSS” format · There are also other formats that the dateTime can be converted ... class Program { static void Main() { DateTime d = DateTime.Now; string dateString = d.ToString("yyyyMMddHHmmss"); System.Console.WriteLine(dateString); Console.ReadLine(); } }
🌐
ASP.NET Forums
forums.asp.net › t › 1678546.aspx
Developer technologies - Microsoft Q&A
A broad category of Microsoft tools, languages, and frameworks for software development. Designed to support developers in building, debugging, and deploying applications across various platforms.
🌐
CodeProject
codeproject.com › Questions › 1189315 › How-to-display-microseconds-of-current-date-time-i
404 - Spoon not Found- CodeProject
Do not try and find the page. That’s impossible. Instead only try to realise the truth - For those who code; Updated: 1 Jul 2007
🌐
UiPath Community
forum.uipath.com › help › studio
How can i append the date and timestamp (including milliseconds) in excel file name - Studio - UiPath Community Forum
April 8, 2020 - Hi, I have code where i have to read emails and put the data into excel file. I am naming excel file with datetime (without milliseconds). But it is overwriting the file because process is completing within seconds, How can i also add ms in my excel name. Below is the current format filePath + fileName + “_” +DateTime.Now.ToString(“yyyyMMddHHmmss”)+”.xlsx"