English 中文(简体)
Cut-/Roundingdown a Daytime to a whole i.e.hour/Day?
原标题:Cut-/Rounding down a DateTime to a Whole i.e. Hour/Day?
  • 时间:2011-11-10 14:22:29
  •  标签:
  • c#

Is there any kind of mathematical way to cut DateTime down to a exact Hour, Day or so? Similiar to round of a decimal to int.

Period.Day
If the original value was 2011-01-01 13:00:00, it ends up in 2011-01-01 00:00:00

if Period.Hour
If the original value was 2011-03-11 13:32:00, it ends up in 2011-03-11 13:00:00

I think about something like below. This are of course works fine, but the range-array are iterated through anyway, later. Better if I was possible to calculate directly on that iteration, instead of it s own. But someType can t be put into that iteration (it depends on someType).

if (someType == Period.Day)
  range.ForEach(d => d.time = new DateTime(d.time.Year, d.time.Month, d.time.Day,0,0,0));
if (someType == Period.Hour)
  range.ForEach(d => d.time = new DateTime(d.time.Year, d.time.Month, d.time.Day, d.time.Hour, 0, 0));
最佳回答

整整整一天等于<代码>。 日期,四舍五入至最接近(中点上限)仅为<代码>(时间+12小时)。

如果四舍五入到一整小时,我就不认为哪一部法律比你更糟。 如果四舍五入到最接近一小时,可将其代码应用于<代码>+30mins。

可能较快的四舍五入方法:

const Int64 HourInTicks=...;
Int64 timeInTicks=time.Ticks;
Int64 trucatedToHour=timeInTicks-timeInTicks%HourInTicks;

但是,我避免了,除非你really需要业绩,否则不可能做到这一点。

(如果你重新利用当地时间,当地时间可能会抵消变化的天数)<>。

问题回答

To round down to day you can use the DateTime.Date Property.
To round down to hour, I m afraid you ll have to either use what you did in your example or something like:

d.Date.AddHours(d.Hour)

I ll do the following:

private static readonly DateTime Epoch = new DateTime(1970, 1, 1); 
public static DateTime Round(this DateTime d, Period p)
{
    var ts = d - Epoch;

    if (p == Period.Hour)
    {
        var hours = (long)ts.TotalHours;
        return Epoch.AddHours(hours);
    }
    else if (p == Period.Days)
    {
        var days = (long)ts.TotalDays;
        return Epoch.AddDays(days);
    }
    // ...
}

我认为,以下C#代码将四舍五入到最接近一分钟的日期,我认为很容易将其推广到其他单位。

//round to nearest minute; add 30 seconds for rounding to nearest minute
effectiveDateTime = effectiveDateTime.AddSeconds(30);
TimeSpan timeComponent = effectiveDateTime.TimeOfDay;
effectiveDateTime = effectiveDateTime.Date;
effectiveDateTime = effectiveDateTime.AddHours(timeComponent.Hours).
     AddMinutes(timeComponent.Minutes);

Not sure if this approach is effective, but looks quite nice using string format (in this case cutting down to hours):

var date = DateTime.UtcNow;
var cutDownDate = Convert.ToDateTime(date.ToString("yyyy-MM-dd hh"));

Answer Is there a better way in C# to round a DateTime to the nearest 5 seconds? contains an excellent generic DateTime rounding approach.

Edit: This answer was before the updated question title and is an algorithm for rounding to nearest not rounding down.

每天的最佳方法:

DateTime now = DateTime.Now;
DateTime roundDay = now.Date;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签