What is the easiest method to remove the Millisecond's part of a DateTime.UctNow.TimeOfDay?

28,899

Solution 1

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);

Solution 2

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);

Solution 3

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;
Share:
28,899
Eon
Author by

Eon

Currently working as an .NET Developer. I am very inquisitive and tend to ask questions mostly on C#, and mostly about the little things that confuse people easily. Proficient in the following Every-Day Languages/Technologies/Applications I use(self rated, could be biased :P, determinate to my level of .NET Development ): ★★★★★ C# ★★★★☆ ASP.NET ★★★★☆ T-SQL ★★★★★ Visual Studio 2008/2010/2012/2013 ★★★★☆ Javascript ★★★★☆ Jquery ★★★★★ HTML ★★★☆☆ Angular JS ★☆☆☆☆ F# (just started learning it) ★★★☆☆ Batch Scripts What I love about coding: Playing with Fire (Cause bugs to figure out what not to do) Optimizing code to reduce Cyclomatic Complexity and increase Maintainability Figuring out Algorithms Teaching Other Developers (though I don't answer a lot of questions on stack overflow due to fear of looking like a fool) DISCOVERY. Always fun to experiment What I hate about coding: Having to deal with people who always think they are right, even when they are wrong or have no context of the current work. Having to deal with purists. Stagnant code Doing 1500 Lines of code realizing I could have done it in around 40 Triple Nested Ternary Expressions (SERIOUSLY, WHO WRITES THOSE?!) Keyboard Cats. I am a Microsoft Certified Professional Developer. Here are my gloatbadges.

Updated on May 04, 2020

Comments

  • Eon
    Eon about 4 years

    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

  • Jon Skeet
    Jon Skeet about 12 years
    There would be no benefit in using a StringBuilder here - it's a single expression, so would be one call to String.Concat.
  • Eon
    Eon about 12 years
    the problem is it is a stored procedure that wants the value as a varchar value, although it is a date. Im forced to really send it as a string. I will keep your post in mind for when I work with Datetimes in databases.
  • Guffa
    Guffa almost 10 years
    Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer.