How to truncate milliseconds off of a .NET DateTime
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Sometimes you want a DateTime rounded down to the nearest whole second for logging, grouping, or equality checks. In .NET, the safest way to do that is to create a new value whose ticks are trimmed to TimeSpan.TicksPerSecond.
That is an important distinction: formatting without milliseconds only changes how the value is displayed. Truncation changes the underlying value itself.
DateTime Stores More Than Milliseconds
DateTime is stored in ticks, where one tick is 100 nanoseconds. So a value like 12:30:15.789 is not really "stored to milliseconds"; it is stored with much finer precision.
If you want to remove the fractional second portion entirely, subtract the remainder after division by TimeSpan.TicksPerSecond.
A Good General Solution
This version truncates to the nearest whole second while preserving the original Kind:
Preserving Kind matters because a DateTime may represent local time, UTC, or an unspecified kind. If you rebuild the value with a constructor that ignores Kind, you can accidentally change later conversion behavior.
AddTicks Is a Nice Readable Alternative
The same logic can be expressed with AddTicks:
This is concise and works well when you already have the source value in hand.
Truncation Is Not the Same as Formatting
Many developers only want output without milliseconds. If so, formatting may be enough:
That prints no milliseconds, but the DateTime still contains them. If you later compare the value, serialize it in a different format, or write it to a database, the fractional seconds are still there.
Use formatting for display. Use tick trimming when the actual stored value must change.
Constructor-Based Rebuilds Work, but Be Careful
A constructor-based approach also works:
This is readable, but more verbose and easier to get wrong if you forget Kind or later want to truncate to a different precision.
DateTimeOffset May Be the Better Type
If your application crosses time zones, DateTimeOffset is often safer because it keeps the offset with the value. Truncation works the same idea:
This does not change the offset. It only removes the fractional second portion.
Why Truncation Is Useful
Common cases include:
- comparing timestamps at second precision
- grouping log entries by whole second
- generating stable string keys
- normalizing values before storage or test assertions
If that is your real requirement, truncation is the correct tool. If you only want prettier output, formatting is simpler.
Common Pitfalls
The most common mistake is confusing formatting with truncation. A custom format string can hide milliseconds visually while leaving the value unchanged.
Another frequent issue is forgetting about Kind. Rebuilding a DateTime without preserving DateTimeKind.Utc or DateTimeKind.Local can create subtle bugs later when time-zone conversions happen.
Developers also mix truncation and rounding. Truncation always moves downward toward the start of the second. If you need nearest-second rounding, the logic is different.
Finally, do not normalize timestamps blindly when sub-second precision actually matters, such as event ordering or high-resolution telemetry.
Summary
- To really remove milliseconds, change the underlying ticks, not just the display format.
- Trimming with
TimeSpan.TicksPerSecondis a clean and precise approach. - Preserve
DateTime.Kindwhen constructing a newDateTime. - Use formatting only when you want cleaner output, not a changed value.
- Consider
DateTimeOffsetwhen time-zone context matters.
Related reading
- How to turn off or handle camelCasing in JSON response ASP.NET Core?
- How to turn off the logging done by the ASP.NET core framework
- How to upload a file to amazon S3 super easy using c
- How to upload a file to amazon S3 super easy using c
- How to use a App.config file in WPF applications?
- How to use async with Visual Studio 2010 and .NET 4.0?
- How to use async/await on void methods in ASP.NET Core?
- How to use AutoMapper .ForMember?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.