61

将 DateTime 对象修剪到特定精度的最佳方法是什么?例如,如果我有一个值为“2008-09-29 09:41:43”的 DateTime,但我只希望它精确到分钟,还有比这更好的方法吗?

private static DateTime TrimDateToMinute(DateTime date)
{
    return new DateTime(
        date.Year, 
        date.Month, 
        date.Day, 
        date.Hour, 
        date.Minute, 
        0);
}

我真正想要的是使其可变,以便我可以将其精度设置为秒、分钟、小时或天。

4

6 回答 6

109
static class Program
{
    //using extension method:
    static DateTime Trim(this DateTime date, long roundTicks)
    {
        return new DateTime(date.Ticks - date.Ticks % roundTicks, date.Kind);
    }

    //sample usage:
    static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now);
        Console.WriteLine(DateTime.Now.Trim(TimeSpan.TicksPerDay));
        Console.WriteLine(DateTime.Now.Trim(TimeSpan.TicksPerHour));
        Console.WriteLine(DateTime.Now.Trim(TimeSpan.TicksPerMillisecond));
        Console.WriteLine(DateTime.Now.Trim(TimeSpan.TicksPerMinute));
        Console.WriteLine(DateTime.Now.Trim(TimeSpan.TicksPerSecond));
        Console.ReadLine();
    }

}
于 2008-09-30T13:26:37.917 回答
9

你可以使用枚举

public enum DateTimePrecision
{
  Hour, Minute, Second
}

public static DateTime TrimDate(DateTime date, DateTimePrecision precision)
{
  switch (precision)
  {
    case DateTimePrecision.Hour:
      return new DateTime(date.Year, date.Month, date.Day, date.Hour, 0, 0);
    case DateTimePrecision.Minute:
      return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, 0);
    case DateTimePrecision.Second:
      return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
    default:
      break;
  }
}

并根据需要扩展。

于 2008-09-30T12:25:25.163 回答
7

我喜欢这种方法。有人提到保留日期类型等很好。这样做是因为您不必创建新的日期时间。DateTime 是从原始 DateTime 正确克隆的,它只是减去剩余的刻度。

public static DateTime FloorTime(DateTime dt, TimeSpan interval) 
{
  return dt.AddTicks(-1 * (dt.Ticks % interval.Ticks));
}

用法:

dt = FloorTime(dt, TimeSpan.FromMinutes(5)); // floor to the nearest 5min interval
dt = FloorTime(dt, TimeSpan.FromSeconds(1)); // floor to the nearest second
dt = FloorTime(dt, TimeSpan.FromDays(1));    // floor to the nearest day
于 2013-04-01T20:17:44.527 回答
1

这里提供了一些很好的解决方案,但是当我需要这样做时,我只是这样做:

DateTime truncDate;
truncDate = date.Date; // trim to day
truncDate = date.Date + TimeSpan.Parse(string.Format("{0:HH:00:00}", date)); // trim to hour
truncDate = date.Date + TimeSpan.Parse(string.Format("{0:HH:mm}", date)); // trim to minute
truncDate = date.Date + TimeSpan.Parse(string.Format("{0:HH:mm:ss}", date)); // trim to second

希望能帮助到你。

于 2014-11-07T18:30:12.133 回答
-1
    static DateTime TrimDate(DateTime date, long roundTicks)
    {
        return new DateTime(date.Ticks - date.Ticks % roundTicks);
    }

    //sample usage:
    static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now);
        Console.WriteLine(TrimDate(DateTime.Now, TimeSpan.TicksPerDay));
        Console.WriteLine(TrimDate(DateTime.Now, TimeSpan.TicksPerHour));
        Console.WriteLine(TrimDate(DateTime.Now, TimeSpan.TicksPerMillisecond));
        Console.WriteLine(TrimDate(DateTime.Now, TimeSpan.TicksPerMinute));
        Console.WriteLine(TrimDate(DateTime.Now, TimeSpan.TicksPerSecond));
        Console.ReadLine();
    }
于 2008-09-30T13:07:47.963 回答
-2
DateTime dt = new DateTime()
dt = dt.AddSeconds(-dt.Second)

上面的代码将修剪秒。

于 2012-06-01T10:49:28.027 回答