C# 如何使用字符串格式将时差舍入到最接近的秒数?
我试图减去2个日期时间,得到分和秒格式的时间差,但是如何将差以字符串格式四舍五入到最接近的秒 我的约会如下: 1)C# 如何使用字符串格式将时差舍入到最接近的秒数?,c#,.net,linq,string.format,C#,.net,Linq,String.format,我试图减去2个日期时间,得到分和秒格式的时间差,但是如何将差以字符串格式四舍五入到最接近的秒 我的约会如下: 1) StartDate= 2016-10-11 04:31:06.513 EndDate= 2016-10-11 04:31:09.457 Differrence : 2.94 Output I am getting is : 00 : 02 Expected Output : 00 : 03 (round up time to the nearest)
StartDate= 2016-10-11 04:31:06.513 EndDate= 2016-10-11 04:31:09.457
Differrence : 2.94
Output I am getting is : 00 : 02
Expected Output : 00 : 03 (round up time to the nearest)
2)
以下是linq查询,我正在尝试计算时差:
var output = Attendance.Select
(
t => new
{
TimeDifference = string.Format("{0:00}:{1:00}", (int)t.EndDateTime.Value.Subtract(t.StartDateTime.Value).Minutes, (int)t.EndDateTime.Value.Subtract(t.StartDateTime.Value).Seconds),
}
).ToList()
您可以为此使用该功能
(int)Math.Ceiling(t.EndDateTime.Value.Subtract(t.StartDateTime.Value).Seconds)
但还有另一个问题。Seconds
属性以整数形式获取完整的秒数。您还需要部分时间,因此正确答案是:
(int)Math.Ceiling(t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalSeconds)
正如Dawnkeeper在他的回答中所说的,您必须使用,但是您必须使用
Seconds
属性,例如:
var TimeDifference = string.Format("{0:00}:{1:00}", (int)EndDateTime.Subtract(StartDateTime).Minutes, (int)Math.Ceiling(EndDateTime.Subtract(StartDateTime).TotalSeconds));
编辑
亨里克是对的,上面的代码不起作用。让我们把它做好:
var diff=EndDateTime.Subtract(StartDateTime);
double seconds= Math.Ceiling(diff.Seconds+diff.Milliseconds*0.001);
var TimeDifference = string.Format("{0:00}:{1:00}",diff.Minutes,seconds);
我现在正在打电话,所以我无法尝试,但我认为它应该可以工作以秒为整数使用TotalSeconds而不是seconds,并用数学将其包围起来。上限:
DateTime startDate = DateTime.Parse("2016-10-14 16:43:18.530");
DateTime endDate = DateTime.Parse("2016-10-14 16:43:50.457 ");
Console.WriteLine( Math.Ceiling(endDate.Subtract(startDate).TotalSeconds));
输出:32您也可以这样做:
DateTime start = new DateTime(2016, 10, 11, 04, 31, 06, 513);
DateTime end = new DateTime(2016, 10, 11, 04, 31, 09, 457);
var diff = end - start;
Console.WriteLine($"{diff.Hours:00}:{diff.Minutes:00}:{diff.Seconds + diff.Milliseconds / 1000.0:00}");
你说的是“围捕”,你会像以前一样在天花板上看到
有人说过。但如果时间紧迫,你会怎么做
2016-10-10 00:00:00.000
2016-10-10 00:00:00.001
是否应将其视为1秒?你的收藏名称是“出勤”,对于出勤系统来说,我想情况并非如此。如果舍入应该是向上或向下或基于中点,则使用Math.Round()。即:
获取错误:以下方法或属性之间的调用不明确:System.Math.天花(double)和System.Math.天花(decimal)将其更正为(int)Math.天花((double)t.EndDateTime.Value.Subtract(t.StartDateTime.Value).Seconds)@Learning fixedit@ToolmakerSteve这正是我们的目标,四舍五入这个答案与Dawnkeeper或我的答案有什么不同?是的,有了这个输入:2016-10-10 00:00:00.000 2016-10-10 00:00:00.001输出应该是这样的:00:01最后一个说“四舍五入”而不是“上限”的人,因为规定的目标是四舍五入@ToolmakerSteve,谢谢。但是OP也把我弄糊涂了。他真的不想要圆形或天花板。他说,即使是一毫秒也可以算作一分钟(如上所述)。我不确定他到底在追求什么。@CetinBasoz-啊哈-我没看到那评论。很奇怪。他问题中的例子有以
.9
结尾的delta,所以我假设他想要显示时间(圆形)时通常需要的东西。啊,好吧,我已经对这个问题发表了评论,这样将来的读者会注意到Round
vs天花
。比提问者想问的问题更重要:)很抱歉写下这篇文章,但如果时间跨度超过1分钟,此解决方案将失败,因为TotalSeconds包括整个时间跨度(年、小时、天、分钟、秒、毫秒)@HenrikHansen:你能给我一个上面的答案会失败的示例输入吗?@Learning:DateTime start=new DateTime(2016,10,11,04,30,06,513);DateTime end=新的日期时间(2016,10,11,04,31,09,457)@亨利坎森:你是对的。这不适用于你的上述输入given@ToolmakerSteve同意。我只是很惊讶,但我当然很高兴你澄清了这个问题,以防有人在这里寻找不同的东西。阅读答案时要注意:因为问题没有说明像3.1
这样的值应该发生什么,大多数答案解释为“四舍五入”意味着“任何分数都应该四舍五入”,于是提出了“数学上限”。例如,数学上限(3.1)=>4
。如果您想“仅对0.5或更大的值进行取整”,则在答案中使用“Math.round
”。
DateTime start = new DateTime(2016, 10, 11, 04, 31, 06, 513);
DateTime end = new DateTime(2016, 10, 11, 04, 31, 09, 457);
var diff = end - start;
Console.WriteLine($"{diff.Hours:00}:{diff.Minutes:00}:{diff.Seconds + diff.Milliseconds / 1000.0:00}");
var output = Attendance.Select
(
t => new
{
TimeDifference =
TimeSpan.FromSeconds(Math.Round(
t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalSeconds, 0))
.ToString(@"mm\:ss")
}
).ToList();