C# 有没有更好的方法将 DateTime 修剪到特定精度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/152774/
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
Is there a better way to trim a DateTime to a specific precision?
提问by Lloyd Cotten
What's the best way to trim a DateTime object to a specific precision? For instance, if I have a DateTime with a value of '2008-09-29 09:41:43', but I only want it's precision to be to the minute, is there any better way to do it than this?
将 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);
}
What I would really want is to make it variable so that I could set its precision to the second, minute, hour, or day.
我真正想要的是使其可变,以便我可以将其精度设置为秒、分、小时或天。
采纳答案by Bartek Szabat
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();
}
}
回答by Rikalous
You could use an enumeration
您可以使用枚举
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;
}
}
and expand as required.
并根据需要进行扩展。
回答by Bartek Szabat
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();
}
回答by Gunarathinam
DateTime dt = new DateTime()
dt = dt.AddSeconds(-dt.Second)
Above code will trim seconds.
上面的代码将修剪秒。
回答by rocketsarefast
I like this method. Someone mentioned it was good to preserve the Date Kind, etc. This accomplishes that because you dont have to make a new DateTime. The DateTime is properly cloned from the original DateTime and it simply subtracts the remainder ticks.
我喜欢这个方法。有人提到保留 Date Kind 等很好。这样做是因为您不必创建新的 DateTime。DateTime 是从原始 DateTime 正确克隆的,它只是减去剩余的刻度。
public static DateTime FloorTime(DateTime dt, TimeSpan interval)
{
return dt.AddTicks(-1 * (dt.Ticks % interval.Ticks));
}
usage:
用法:
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
回答by Marcos Arruda
There are some good solutions presented here, but when I need to do this, I simply do:
这里提供了一些很好的解决方案,但是当我需要这样做时,我只是这样做:
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
Hope it helps.
希望能帮助到你。