I'm trying to compare a time stamp from an incoming request to a database stored value. SQL Server of course keeps some precision of milliseconds on the time, and when read into a .NET DateTime, it includes those milliseconds. The incoming request to the system, however, does not offer that precision, so I need to simply drop the milliseconds.
I feel like I'm missing something obvious, but I haven't found an elegant way to do it (C#).
To round down to the second:
Replace with
TicksPerMinute
to round down to the minute.If your code is performance sensitive, be cautious about
My app was spending 12% of CPU time in System.DateTime.GetDatePart.
Sometimes you want to truncate to something calendar-based, like year or month. Here's an extension method that lets you choose any resolution.
Regarding Diadistis response. This worked for me, except I had to use Floor to remove the fractional part of the division before the multiplication. So,
becomes
I would have expected the division of two Long values to result in a Long, thus removing the decimal part, but it resolves it as a Double leaving the exact same value after the multiplication.
Eppsy
Here is an extension method based on a previous answer that will let you truncate to any resolution...
Usage:
Class:
New Method
// define String pass parameter dd-mmm-yyyy return 24-feb-2016
Or shown on textbox
// put on PageonLoad