How can I get the correct payperiod from the date?

I feel that this is a mathematical problem the most. My company has employees all over the country. Some parts of the company are in an “odd” cycle of payment, and some are “even”. I call the start date of this payment period a "maturity period." I need to do two things:

1) determine the payment period during which this date falls

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

2) get a list of payment periods between two dates:

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

I use a couple of dates as fixed standards on which to base future payment dates. Fixed standard dates for odd and even:

  • Even - 01/04/09
  • Odd - 01/11/09

. , , 01.04.09 01/17/09. 01/11/09 01/24/09. , . , .

, , "" . , .

+3
5

, , :

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();
}
+3

, , :

public static DateTime getPayPeriodStartDate(DateTime givenDate, string EvenOrOdd)
{
    DateTime newYearsDay = new DateTime(DateTime.Today.Year, 1, 1);
    DateTime firstEvenMonday = newYearsDay.AddDays((8 - (int)newYearsDay.DayOfWeek) % 7);
    DateTime firstOddMonday = firstEvenMonday.AddDays(7);
    TimeSpan span = givenDate - (EvenOrOdd.Equals("Even") ? firstEvenMonday : firstOddMonday);
    int numberOfPayPeriodsPast = span.Days / 14;
    return (EvenOrOdd.Equals("Even") ? firstEvenMonday : firstOddMonday).AddDays(14 * numberOfPayPeriodsPast);
}

public static List<DateTime> getPayPeriodsBetween(DateTime start, DateTime end, string EvenOrOdd)
{
    DateTime currentPayPeriod = getPayPeriodStartDate(start, EvenOrOdd);
    if (currentPayPeriod < start) currentPayPeriod = currentPayPeriod.AddDays(14);
    List<DateTime> dtList = new List<DateTime>();
    while (currentPayPeriod <= end)
    {
        dtList.Add(currentPayPeriod);
        currentPayPeriod = currentPayPeriod.AddDays(14);
    }
    return dtList;
}

, .

+1

- , , LINQ. , /, . , emum , EvenOrOdd, .

0

, script , . , , .

( ) , , . , , ( ).

, , , .

, 1: 1/4/2009 1/11/2009 ( / ) 2 , , + 2 . .

For number 2: Same thing, start from the date and add 2 weeks until you reach the date range. While you are there, add each item to the list. As soon as you finish the last date, exit the loop and return a new brilliant list.

If you used my method and went with a database to host all this information, it turns into 2 simple queries:

1) SELECT * FROM payperiods WHERE startdate<=givenDate ORDER BY startdate LIMIT 1

2) SELECT * FROM payperiods WHERE startdate>=givenDate AND enddate<=givenDate ORDER BY startdate

0
source

It works great. I have tested.

 public static DateTime GetFirstDayOfWeek(DateTime dayInWeek)
   {

    CultureInfo _culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    CultureInfo _uiculture = (CultureInfo)CultureInfo.CurrentUICulture.Clone();

    _culture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;
    _uiculture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;

    System.Threading.Thread.CurrentThread.CurrentCulture = _culture;
    System.Threading.Thread.CurrentThread.CurrentUICulture = _uiculture;

    // CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
    DayOfWeek firstDay = _culture.DateTimeFormat.FirstDayOfWeek;
    DateTime firstDayInWeek = dayInWeek.Date;

    // Logic Of getting pay period Monday(Odd monday)

    int i = Convert.ToInt32(firstDay);
    while (firstDayInWeek.DayOfWeek != firstDay)
        if (i % 2 != 0)
        { firstDayInWeek = firstDayInWeek.AddDays(-1); }
        else
        {
            firstDayInWeek = firstDayInWeek.AddDays(-2);
        }
    return firstDayInWeek;
}
0
source

Source: https://habr.com/ru/post/1731073/


All Articles