如何每10分钟四舍五入日期时间

时间:2019-03-17 16:37:22

标签: c# .net-core

在DateTime对象的C#上四舍五入的最佳方法是什么?就是这种情况:

  • 当我在10h:1100万时,我需要将其转换为10h:10mn
  • 当我处于10h:1900万时,我需要将其转换为10h:10mn
  • 当我处于10h:10mn时,我需要将其转换为10h:10mn
  • 当我处于10h:2500万时,我需要将其转换为10h:20mn

我开始像这样测试舍入:

public static DateTime RoundDown(this DateTime dt, TimeSpan d)
{
    var delta = dt.Ticks % d.Ticks;

    return new DateTime(dt.Ticks - delta, dt.Kind);
}

3 个答案:

答案 0 :(得分:0)

这将完成工作,但仅在最近的10分钟间隔内进行。为了使其更通用,需要使用一个Timespan作为参数来实现,以定义日期时间的舍入级别。

    public DateTime RoundDateTimeMinutes(DateTime date, bool roundUp = true)
    {
        var minutesDiff = date.Minute % 10;

        if (minutesDiff == 5)
        {
            if (roundUp)
            {
                date = date.AddMinutes(minutesDiff);
            }
            else
            {
                date = date.AddMinutes(-minutesDiff);
            }
        }            
        else if (minutesDiff > 5)
        {
            date = date.AddMinutes(10 - minutesDiff);
        }
        else
        {
            date = date.AddMinutes(-minutesDiff);
        }

        return date;
    }

编辑:实际上,以上结果四舍五入为最近的10分钟间隔。这是总是四舍五入的实际方法。

    public DateTime RoundDateTimeMinutes(DateTime date)
    {
        var minutesDiff = date.Minute % 10;
        return date.AddMinutes(-minutesDiff);            
    }

答案 1 :(得分:0)

这似乎是一个无答案的答案,但我不知道您如何才能改善已有的知识。它有效,简洁,灵活(没有硬编码为特定的时间间隔),可缩短一分钟的时间并保持Kind属性。您还需要什么?

测试代码:

static void Main(string[] args)
{
    TestRoundingForHourAfter(DateTime.Parse("10 AM"));
    TestRoundingForHourAfter(DateTime.Parse("10 AM").AddTicks(123456789));
}

static void TestRoundingForHourAfter(DateTime baseTime)
{
    foreach (DateTime input in Enumerable.Range(0, 60).Select(minutes => baseTime + TimeSpan.FromMinutes(minutes)))
    {
        DateTime output = RoundDown(input, TimeSpan.FromMinutes(10));

        Console.WriteLine($"{input:hh:mm:ss.fffffff} rounds to {output:hh:mm:ss.fffffff}");
    }
}

public static DateTime RoundDown(DateTime dt, TimeSpan d)
{
    var delta = dt.Ticks % d.Ticks;

    return new DateTime(dt.Ticks - delta, dt.Kind);
}

测试输出:

10:00:00.0000000 rounds to 10:00:00.0000000
10:01:00.0000000 rounds to 10:00:00.0000000
10:02:00.0000000 rounds to 10:00:00.0000000
10:03:00.0000000 rounds to 10:00:00.0000000
10:04:00.0000000 rounds to 10:00:00.0000000
10:05:00.0000000 rounds to 10:00:00.0000000
10:06:00.0000000 rounds to 10:00:00.0000000
10:07:00.0000000 rounds to 10:00:00.0000000
10:08:00.0000000 rounds to 10:00:00.0000000
10:09:00.0000000 rounds to 10:00:00.0000000
10:10:00.0000000 rounds to 10:10:00.0000000
...
10:00:12.3456789 rounds to 10:00:00.0000000
10:01:12.3456789 rounds to 10:00:00.0000000
10:02:12.3456789 rounds to 10:00:00.0000000
10:03:12.3456789 rounds to 10:00:00.0000000
10:04:12.3456789 rounds to 10:00:00.0000000
10:05:12.3456789 rounds to 10:00:00.0000000
10:06:12.3456789 rounds to 10:00:00.0000000
10:07:12.3456789 rounds to 10:00:00.0000000
10:08:12.3456789 rounds to 10:00:00.0000000
10:09:12.3456789 rounds to 10:00:00.0000000
10:10:12.3456789 rounds to 10:10:00.0000000
...

答案 2 :(得分:-1)

仅构造一个新的DateTime,并传递未舍入的DateTime的年,月,日和小时。分钟,将原始分钟以10为模,秒后通过0。

var rounded = new DateTime
(
    original.Year,
    original.Month,
    original.Day,
    original.Hour,
    Math.Floor(original.Minute / 10) * 10
    0
);