C# 将时间分成小时块

发布于 2024-11-11 16:03:17 字数 542 浏览 0 评论 0原文

我需要一些帮助来将两个日期时间分成它们之间的小时间隔。

这是与“付费”数据一起使用的,因此它需要非常准确。我需要打卡上班和下班,并将它们分成小时间隔。

示例:

打卡 = 5/25/2011 1:40:56PM

打卡 = 5/25/2011 6:22:12PM

我需要看起来像:

5/25 /2011 1:40:56PM

5/25/2011 2:00:00PM

5/25/2011 3:00:00PM

5/25/2011 4:00:00PM

5/25/2011 5:00:00PM

5/25 /2011 6:00:00PM

5/25/2011 6:22:12PM

然后我计划根据“差异”表检查这些时间,看看他们是否应该有一个新的支付代码。但我稍后会担心支付代码。

有什么帮助分散时间吗?更喜欢 C#,但我也可以访问 MSSQL2000(这是我们拉原始时代的地方)

I am in need of some assistance in getting 2 datetimes to split into the hour intervals between them.

This is working with 'pay' data, so it needs to be very accurate. I need to take clockin and clockout, and split them into hour intervals.

Example:

clockin = 5/25/2011 1:40:56PM

clockout = 5/25/2011 6:22:12PM

I need that to look like:

5/25/2011 1:40:56PM

5/25/2011 2:00:00PM

5/25/2011 3:00:00PM

5/25/2011 4:00:00PM

5/25/2011 5:00:00PM

5/25/2011 6:00:00PM

5/25/2011 6:22:12PM

I then plan to check those times against a 'differential' table to see fi they should have a new paycode. But I'll worry about the paycode later.

Any help splitting out the times? Prefer C#, but I also have access to MSSQL2000 (which is where we pull the original times)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谈情不如逗狗 2024-11-18 16:03:17

像这样的事情怎么样?

static IEnumerable<DateTime> GetWorkingHourIntervals(DateTime clockIn, DateTime clockOut)
{
    yield return clockIn;

    DateTime d = new DateTime(clockIn.Year, clockIn.Month, clockIn.Day, clockIn.Hour, 0, 0, clockIn.Kind).AddHours(1);

    while (d < clockOut)
    {
        yield return d;
        d = d.AddHours(1);
    }

    yield return clockOut;
}

这使用 迭代器块 但它可以很容易地重写以返回一个列表。

使用示例:

static void Main(string[] args)
{
    var clockIn = new DateTime(2011, 5, 25, 13, 40, 56);
    var clockOut = new DateTime(2011, 5, 25, 18, 22, 12);

    var hours = GetWorkingHourIntervals(clockIn, clockOut);

    foreach (var h in hours)
        Console.WriteLine(h);

    Console.ReadLine();
}

输出:

2011-05-25 13:40:56
2011-05-25 14:00:00
2011-05-25 15:00:00
2011-05-25 16:00:00
2011-05-25 17:00:00
2011-05-25 18:00:00
2011-05-25 18:22:12

更新LukeH< /a> 很聪明地建议您还应该复制 DateTimeKind。如果您计划稍后将日期时间与本地时间相互转换,这确实是一个明智之举。

How about something like this?

static IEnumerable<DateTime> GetWorkingHourIntervals(DateTime clockIn, DateTime clockOut)
{
    yield return clockIn;

    DateTime d = new DateTime(clockIn.Year, clockIn.Month, clockIn.Day, clockIn.Hour, 0, 0, clockIn.Kind).AddHours(1);

    while (d < clockOut)
    {
        yield return d;
        d = d.AddHours(1);
    }

    yield return clockOut;
}

This uses iterator blocks but it could easily be rewritten to return a list instead.

Example use:

static void Main(string[] args)
{
    var clockIn = new DateTime(2011, 5, 25, 13, 40, 56);
    var clockOut = new DateTime(2011, 5, 25, 18, 22, 12);

    var hours = GetWorkingHourIntervals(clockIn, clockOut);

    foreach (var h in hours)
        Console.WriteLine(h);

    Console.ReadLine();
}

Output:

2011-05-25 13:40:56
2011-05-25 14:00:00
2011-05-25 15:00:00
2011-05-25 16:00:00
2011-05-25 17:00:00
2011-05-25 18:00:00
2011-05-25 18:22:12

Update: LukeH was clever enough to suggest that you should also copy the DateTimeKind. This is indeed a smart move if you're planning on converting the datetimes to/from local time later on.

本宫微胖 2024-11-18 16:03:17
var hours = new List<DateTime>();
hours.Add(clockin);

var next = new DateTime(clockin.Year, clockin.Month, clockin.Day,
                        clockin.Hour, 0, 0, clockin.Kind);

while ((next = next.AddHours(1)) < clockout)
{
    hours.Add(next);
}
hours.Add(clockout);
var hours = new List<DateTime>();
hours.Add(clockin);

var next = new DateTime(clockin.Year, clockin.Month, clockin.Day,
                        clockin.Hour, 0, 0, clockin.Kind);

while ((next = next.AddHours(1)) < clockout)
{
    hours.Add(next);
}
hours.Add(clockout);
ぽ尐不点ル 2024-11-18 16:03:17

我认为这样的事情应该有效:

public IEnumerable<DateTime> GetHourlyBreakdown(DateTime startDate, DateTime endDate)
{
    var hours = new List<DateTime>();
    hours.Add(startDate);
    var currentDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, startDate.Hour, 0, 0).AddHours(1);
    while(currentDate < endDate)
    {
        hours.Add(new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, currentDate.Hour, 0, 0));
        currentDate = currentDate.AddHours(1);
    }
    hours.Add(endDate);
    return hours;
}

I think something like this should work:

public IEnumerable<DateTime> GetHourlyBreakdown(DateTime startDate, DateTime endDate)
{
    var hours = new List<DateTime>();
    hours.Add(startDate);
    var currentDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, startDate.Hour, 0, 0).AddHours(1);
    while(currentDate < endDate)
    {
        hours.Add(new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, currentDate.Hour, 0, 0));
        currentDate = currentDate.AddHours(1);
    }
    hours.Add(endDate);
    return hours;
}
转瞬即逝 2024-11-18 16:03:17

我会这样做:

public static IEnumerable<DateTime> GetIntervals(DateTime clockIn, DateTime clockOut)
{
    yield return clockIn;

    clockIn = clockIn.AddHours(1).Subtract(clockIn.TimeOfDay).AddHours(clockIn.Hour);

    for (DateTime dt = clockIn; dt < clockOut; dt = dt.AddHours(1))
        yield return dt;

    yield return clockOut;
}

像这样使用它:

    foreach (DateTime dt in GetIntervals(DateTime.Parse("5/25/2011 1:40:56PM", CultureInfo.InvariantCulture), DateTime.Parse("5/25/2011 6:22:12PM", CultureInfo.InvariantCulture)))
    {
        Console.WriteLine(dt);
    }

I would do this:

public static IEnumerable<DateTime> GetIntervals(DateTime clockIn, DateTime clockOut)
{
    yield return clockIn;

    clockIn = clockIn.AddHours(1).Subtract(clockIn.TimeOfDay).AddHours(clockIn.Hour);

    for (DateTime dt = clockIn; dt < clockOut; dt = dt.AddHours(1))
        yield return dt;

    yield return clockOut;
}

Use it like this:

    foreach (DateTime dt in GetIntervals(DateTime.Parse("5/25/2011 1:40:56PM", CultureInfo.InvariantCulture), DateTime.Parse("5/25/2011 6:22:12PM", CultureInfo.InvariantCulture)))
    {
        Console.WriteLine(dt);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文