C# 删除 DateTime.UctNow.TimeOfDay 的毫秒部分的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9768208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:46:23  来源:igfitidea点击:

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

c#datetimecasting

提问by Eon

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.Millisecondswill subtract the exact amount of milliseconds specified by the DateTime.UtcNow.TimeOfDay

处理器必须计算DateTime.UtcNow.TimeOfDay.Milliseconds,并且由于 CPU 单个滴答的时间长度(以及处理此属性并返回结果的时间),它并不表示DateTime.UtcNow.TimeOfDay.Milliseconds将减去由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?

我需要知道,在DateTime.UtcNow.TimeOfDay无需使用大量处理器时间的情况下,从 中删除毫秒数的最简单和最有效的方法是什么?我的这个应用很大,这个问题很简单。但是当这个应用程序被部署时,它就没有不稳定的空间了。这个毫秒必须被修整,因为它被发送到 SQL Server 中的一个存储过程,而这个特定的存储过程不支持 DateTimes 上的毫秒。我也经常遇到这个问题,但我通常将日期转换为字符串(这本身就是一个强制转换),以毫秒为单位在句号处拆分字符串,并使用索引位置 0 来获取我需要的时间。有没有更短、更有效的方法?

Stability and speed is most important to me.

稳定性和速度对我来说是最重要的。

Thanks in advance

提前致谢

采纳答案by Eon

Everything you need to know about customising the DateTime ToString format is here on MSDN.

您需要了解的有关自定义DateTime ToString 格式的所有信息都在 MSDN 上。

In simple terms, something like this:

简单来说,是这样的:

endDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd hh:mm:ss"); 

(alter the format as desired)

(根据需要更改格式)

回答by Guffa

Don't repeatedly use the Now/UtcNowproperty in the same expression. Get the value once, and use the same value in the different places:

不要在同一个表达式中重复使用Now/UtcNow属性。获取一次值,在不同的地方使用相同的值:

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:

如果您只想以特殊方式格式化日期,而不需要实际的 DateTime 值,则可以跳过格式中的毫秒数,例如:

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 值发送:

DateTime now = DateTime.Now;
DateTime endDate = now - new TimeSpan(0, 0, 0, 0, now.TimeOfDay.Milliseconds);

回答by Indy9000

Use C# DateTime formattingas described very well in the MSDN. Your analysis on milisecond calculation is quite possibly wrong. Also for string concatenation use a StringBuilder

使用MSDN 中很好地描述的C# DateTime格式。您对毫秒计算的分析很可能是错误的。同样用于字符串连接使用 StringBuilder

回答by Christian.K

Basically, you create a new DateTimeinstance from an existing one, but set everything "smaller" then Milliseconds to zero. You can use an extensions method:

基本上,您DateTime从现有实例创建一个新实例,但将所有内容“更小”然后设置为零。您可以使用扩展方法:

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;

回答by kirk

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