如何使用字符串格式将时差四舍五入到最接近的秒数?
How to round up time difference to the nearest seconds with 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)
2)
StartDate = 2016-10-14 16:43:18.530 EndDate= 2016-10-14 16:43:50.457
Difference : 31.93
Output I am getting is : 00 : 31
Expected Output : 00 : 32 (round up time to the nearest)
这是下面的 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()
您可以为此使用 Math.Ceiling()
函数。
(int)Math.Ceiling(t.EndDateTime.Value.Subtract(t.StartDateTime.Value).Seconds)
但是还有一个问题。 Seconds
属性 得到整数秒。您还需要部分秒数,因此正确答案是:
(int)Math.Ceiling(t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalSeconds)
正如 Dawnkeeper 在他的回答中所说,你必须使用 Math.Ceiling, but instead getting the Seconds
property, you must use TimeSpan.TotalSeconds,像这样:
var TimeDifference = string.Format("{0:00}:{1:00}", (int)EndDateTime.Subtract(StartDateTime).Minutes, (int)Math.Ceiling(EndDateTime.Subtract(StartDateTime).TotalSeconds));
编辑
Henrik 是对的,上面的代码不起作用。让我们做对吧:
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);
我现在还在 phone,所以我不能尝试,但我认为它应该可以工作
要舍入秒数,请使用 TotalSeconds 而不是 Seconds,并用 Math.Ceiling:
将其包围
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}");
你说的是 "round up",你会用 Ceiling 得到它,因为已经有
被说。但是如果时间是你会怎么做?:
2016-10-10 00:00:00.000
2016-10-10 00:00:00.001
应该算1秒吗?您的集合名称是出勤,我想对于出勤系统来说情况并非如此。如果四舍五入应该向上或向下或基于中点,则使用 Math.Round()。即:
var output = Attendance.Select
(
t => new
{
TimeDifference =
TimeSpan.FromSeconds(Math.Round(
t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalSeconds, 0))
.ToString(@"mm\:ss")
}
).ToList();
我正在尝试减去 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)
2)
StartDate = 2016-10-14 16:43:18.530 EndDate= 2016-10-14 16:43:50.457
Difference : 31.93
Output I am getting is : 00 : 31
Expected Output : 00 : 32 (round up time to the nearest)
这是下面的 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()
您可以为此使用 Math.Ceiling()
函数。
(int)Math.Ceiling(t.EndDateTime.Value.Subtract(t.StartDateTime.Value).Seconds)
但是还有一个问题。 Seconds
属性 得到整数秒。您还需要部分秒数,因此正确答案是:
(int)Math.Ceiling(t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalSeconds)
正如 Dawnkeeper 在他的回答中所说,你必须使用 Math.Ceiling, but instead getting the Seconds
property, you must use TimeSpan.TotalSeconds,像这样:
var TimeDifference = string.Format("{0:00}:{1:00}", (int)EndDateTime.Subtract(StartDateTime).Minutes, (int)Math.Ceiling(EndDateTime.Subtract(StartDateTime).TotalSeconds));
编辑 Henrik 是对的,上面的代码不起作用。让我们做对吧:
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);
我现在还在 phone,所以我不能尝试,但我认为它应该可以工作
要舍入秒数,请使用 TotalSeconds 而不是 Seconds,并用 Math.Ceiling:
将其包围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}");
你说的是 "round up",你会用 Ceiling 得到它,因为已经有 被说。但是如果时间是你会怎么做?:
2016-10-10 00:00:00.000 2016-10-10 00:00:00.001
应该算1秒吗?您的集合名称是出勤,我想对于出勤系统来说情况并非如此。如果四舍五入应该向上或向下或基于中点,则使用 Math.Round()。即:
var output = Attendance.Select
(
t => new
{
TimeDifference =
TimeSpan.FromSeconds(Math.Round(
t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalSeconds, 0))
.ToString(@"mm\:ss")
}
).ToList();