我怎样才能得到正确的日期payperiod? [英] How can I get correct payperiod from date?

查看:153
本文介绍了我怎样才能得到正确的日期payperiod?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是数学题比什么都重要。我公司现有员工遍布全国。该公司的某些部分是在一个奇支付周期的,有些是在偶。我打电话给定薪期payperiod的起始日期。我需要做两件事情:

I feel like this is math problem more than anything. My company has employees all over the country. Some parts of the company are on an "odd" pay cycle and some are on "even". I call the starting date of a given pay period a "payperiod". I need to do two things:

1)决定给定的日期落在

1) determine the payperiod in which a given date falls

//Something like this:
public static DateTime getPayPeriodStartDate(DateTime givenDate, string EvenOrOdd)
{ .. }

2)得到两个日期之间payperiods的列表:

2) get a list of payperiods between two dates:

//Something like this:
public static List<DateTime> getPayPeriodsBetween(DateTime start, DateTime end, string EvenOrOdd)
{ .. }



我中号使用几个日期固定的标准,以此为基础的任何未来的支付周期的日期。是奇数和偶数的固定的标准日期如下:

I'm using a couple dates as fixed standards on which to base any future pay period dates. The fixed standard dates for even and odd are as follows:


  • 即使 - 09年1月4日

  • 奇 - 01/11/09

每个支付一周的周日期开始,去了两个星期。例如,使用上述的标准日期,第一连付期从09年1月4日和01/17/09结束。第一奇工资期从01/11/09和09年1月24日结束。正如你所看到的,有一些重叠。我们拥有数千名员工因此有必要将它们分开了一点。

Each pay period starts on the sunday of the week and goes for two weeks. For instance, using the standard dates above, the first even pay period starts on 01/04/09 and ends on 01/17/09. The first odd pay period starts on 01/11/09 and ends on 01/24/09. As you can see, there is some overlap. We have thousands of employees so it's necessary to split them up a bit.

我有一个基于周数的解决方案,但它笨重,已被固定每个新的一年。我想知道你将如何处理这个

I have a solution that is based on week numbers but it's clunky and has to be "fixed" every new year. I'm wondering how you would handle this.

推荐答案

没有完全优化和测试,但是这是我想出了:

Not fully optimized or tested, but this is what I came up with:

const int DaysInPeriod = 14;

static IEnumerable<DateTime> GetPayPeriodsInRange(DateTime start, DateTime end, bool isOdd)
{
    var epoch = isOdd ? new DateTime(2009, 11, 1) : new DateTime(2009, 4, 1);
    var periodsTilStart = Math.Floor(((start - epoch).TotalDays) / DaysInPeriod);

    var next = epoch.AddDays(periodsTilStart * DaysInPeriod);

    if (next < start) next = next.AddDays(DaysInPeriod);

    while (next <= end)
    {
        yield return next;
        next = next.AddDays(DaysInPeriod);
    }

    yield break;
}

static DateTime GetPayPeriodStartDate(DateTime givenDate, bool isOdd)
{
    var candidatePeriods = GetPayPeriodsInRange(givenDate.AddDays(-DaysInPeriod), givenDate.AddDays(DaysInPeriod), isOdd);
    var period = from p in candidatePeriods where (p <= givenDate) && (givenDate < p.AddDays(DaysInPeriod)) select p;
    return period.First();
}

这篇关于我怎样才能得到正确的日期payperiod?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆