user3590485
user3590485

Reputation: 241

How to get ByWeekly Pay Period in c#?

I have this code for getting First Day of week according to Today's date.

CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
DayOfWeek firstDay = _culture.DateTimeFormat.FirstDayOfWeek;
DateTime firstDayInWeek = dayInWeek.Date;
while (firstDayInWeek.DayOfWeek != firstDay)
    firstDayInWeek = firstDayInWeek.AddDays(-1);
return firstDayInWeek;

How to return biweeklyFirstDay here?

Ex: Today's date is 27 May 2014. The result I get here is

firstDayInWeek = 26 May 2014.

But what I need is 19 May 2014.

Simply put, I want to return 19 May, 2 June, 16 June, 30 June, 14 July, 28 July, 11 August according to System's Today's Date.

Any example would be appreciated.

Upvotes: 2

Views: 2977

Answers (4)

Sam Axe
Sam Axe

Reputation: 33738

    static void Main(string[] args)
    {
        DayOfWeek firstDayOfWeek = DayOfWeek.Monday;
        DateTime sunday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);
        DateTime firstDay = sunday.AddDays((int)firstDayOfWeek);
        DateTime value = firstDay;

        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(value.ToShortDateString());
            value = value.AddDays(14);
        }

    }

Upvotes: 1

JMK
JMK

Reputation: 28080

You want something like this:

IEnumerable<DateTime> Payday(DateTime startDate, int howMany)
{
    int count = 0, mondays = 0;

    while (count < howMany)
    {
        startDate = startDate.AddDays(1);

        if (startDate.DayOfWeek == DayOfWeek.Monday && mondays == 0)
        {
            mondays++;
        }
        else if (startDate.DayOfWeek == DayOfWeek.Monday)
        {
            mondays = 0;
            count++;
            yield return startDate;
        }
    }
}

When called like so:

var payDates = Payday(DateTime.Now, 6);

payDates is equal to the next 6 pay dates, assuming you started working today.

When I use this in a console application, I get the following output:

Output

Upvotes: 2

Alex M
Alex M

Reputation: 720

int delta = _culture.DateTimeFormat.FirstDayOfWeek - DateTime.Now.DayOfWeek;
while... // some criterion for continuing your quest for biweekly start days is true
{
    DateTime monday = DateTime.Now.AddDays(delta);
    delta += 14;
    // do something with each biweekly "monday" here
}

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66501

Use this code to get the week number that includes firstDayInWeek.

var calendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;

var week = calendar.GetWeekOfYear(firstDayInWeek, CalendarWeekRule.FirstDay, firstDay);

Then divide it by 2 and see if the remainder is 0 (indicating whether it's an even or odd number week - i.e week 18 vs 19).

The date you specified falls in week 21, so the following code detects that that's not divisible by 2, and subtracts 7 days, giving you the first day in week 20.

if (currentWeek % 2 != 0)
    firstDayInWeek = firstDayInWeek.AddDays(-7);

Upvotes: 2

Related Questions