Home > Backend Development > C++ > How to Efficiently Truncate Milliseconds from a .NET DateTime Object?

How to Efficiently Truncate Milliseconds from a .NET DateTime Object?

Patricia Arquette
Release: 2025-01-18 22:19:08
Original
321 people have browsed it

How to Efficiently Truncate Milliseconds from a .NET DateTime Object?

Handling Millisecond Discrepancies in .NET DateTime Objects

When comparing timestamps received from external requests with database values (e.g., SQL Server), precision differences can lead to comparison issues. This often stems from the database's millisecond precision, which may not be present in the incoming data. To rectify this, removing milliseconds from the .NET DateTime object is necessary.

A concise solution involves directly manipulating the Ticks property:

<code class="language-csharp">DateTime dateTime = ...; // Your DateTime object
dateTime = new DateTime(dateTime.Ticks - (dateTime.Ticks % TimeSpan.TicksPerSecond), dateTime.Kind);</code>
Copy after login

Alternatively, a slightly more readable version achieves the same result:

<code class="language-csharp">dateTime = dateTime.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerSecond));</code>
Copy after login

Both methods effectively truncate milliseconds while preserving the Kind property (Local, Utc, or Unspecified).

For enhanced reusability, consider creating an extension method:

<code class="language-csharp">public static DateTime TruncateMilliseconds(this DateTime dateTime, TimeSpan timeSpan)
{
    if (timeSpan == TimeSpan.Zero) return dateTime; // Handle zero TimeSpan

    if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) return dateTime; // Preserve special values

    return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks));
}</code>
Copy after login

This extension method allows for flexible truncation:

<code class="language-csharp">dateTime = dateTime.TruncateMilliseconds(TimeSpan.FromMilliseconds(1)); // Truncate to whole milliseconds
dateTime = dateTime.TruncateMilliseconds(TimeSpan.FromSeconds(1));     // Truncate to whole seconds
dateTime = dateTime.TruncateMilliseconds(TimeSpan.FromMinutes(1));    // Truncate to whole minutes
// ... and so on</code>
Copy after login

These methods provide straightforward ways to eliminate millisecond discrepancies when comparing DateTime objects, ensuring accurate timestamp comparisons.

The above is the detailed content of How to Efficiently Truncate Milliseconds from a .NET DateTime Object?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template