Reputation: 3974
I have a theory why the following code is not producing the results I need:
endDate = DateTime.UtcNow.AddDays(1).ToShortDateString() + " " +
DateTime.UtcNow.TimeOfDay.Subtract(
new TimeSpan(0, 0, 0, 0, DateTime.UtcNow.TimeOfDay.Milliseconds));
The processor must calculate the DateTime.UtcNow.TimeOfDay.Milliseconds
, and due to the time length of a single tick of the CPU (and the time to process this property and return a result), It does not denote that DateTime.UtcNow.TimeOfDay.Milliseconds
will subtract the exact amount of milliseconds specified by the DateTime.UtcNow.TimeOfDay
I need to know, what is the simplest and most effective method to remove the amount of milliseconds from DateTime.UtcNow.TimeOfDay
, without having to use a huge amount of processor time? This application of mine is pretty big, and this problem is pretty simple. But when this application gets deployed, there are no room for it to be unstable. This milliseconds must be trimmed because it is sent to a stored procedure in SQL Server, and this specific stored procedure does not support milliseconds on DateTimes. I also run into this problem commonly, but I normally convert the date to string (which is a cast on its own), split the string at the full stop at milliseconds, and use the index position 0 to get the time i need. Is there a shorter, more effective way?
Stability and speed is most important to me.
Thanks in advance
Upvotes: 8
Views: 34074
Reputation: 1007
If you want to remove milliseconds without having any problem on ticks.
DateTime d = DateTime.Now;
var newDate = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
Upvotes: 8
Reputation: 49280
Basically, you create a new DateTime
instance from an existing one, but set everything "smaller" then Milliseconds to zero. You can use an extensions method:
public static class DateTimeExtensions
{
public static DateTime ZeroMilliseconds(this DateTime dt)
{
return new DateTime(((dt.Ticks / 10000000) * 10000000), dt.Kind);
}
}
Or for a full example using your code:
var now = DateTime.Now;
endDate = now.AddDays(1).ToShortDateString() + " " + now.ZeroMilliseconds().TimeOfDay;
Upvotes: 4
Reputation: 8871
Use C# DateTime formatting as described very well in the MSDN. Your analysis on milisecond calculation is quite possibly wrong. Also for string concatenation use a StringBuilder
Upvotes: -1
Reputation: 700562
Don't repeatedly use the Now
/UtcNow
property in the same expression. Get the value once, and use the same value in the different places:
DateTime now = DateTime.Now;
endDate = now.AddDays(1).ToShortDateString() + " " +
now.TimeOfDay.Subtract(
new TimeSpan(0, 0, 0, 0, now.TimeOfDay.Milliseconds));
If you only want the date formatted in a special way, and don't need the actual DateTime value, you can just skip the milliseconds in the format, for example:
endDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
As you are sending the value to the database, you should not send it as a string, but as a DateTime value:
DateTime now = DateTime.Now;
DateTime endDate = now - new TimeSpan(0, 0, 0, 0, now.TimeOfDay.Milliseconds);
Upvotes: 9
Reputation:
Everything you need to know about customising the DateTime ToString format is here on MSDN.
In simple terms, something like this:
endDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd hh:mm:ss");
(alter the format as desired)
Upvotes: 8