Can anyone help me?

I'm trying to display a date range from a Saturday to the following next Friday (every two weeks) in an asp:listbox for the past 12 months. I.e., would show something like:
9/11/2010 - 9/24/2010
8/28/2010 - 9/10/2010
8/14/2010 - 8/27/2010
etc...

I'm using asp.net c#.

Any help is MUCH appreciated!!!

Recommended Answers

All 9 Replies

Show some code of what you have tried so far, explain what/where the problem is and someone might help you.

you can use DateTime and some conditions.
Tell your problem exactly.
Show some of your effort at least for us to help you.

OK... sorry I didn't post any of my c# code... It's a bit complicated, but here goes:

code behind:

 protected void PayPeriod()
        {
            ArrayList pp_select_array = new ArrayList();
            DateTime pay_period_begin = DateTime.Parse(Session["pp_default"].ToString());
            DateTime fixedDate = new DateTime(2008, 09, 27); // Some starting Saturday Pay Period //
            DateTime Today = DateTime.Now;
            DateTime thisSat = new DateTime();
            DateTime MenuSat = new DateTime();
            Microsoft.VisualBasic.FirstDayOfWeek z1 = 0;
            int dayOweek = Convert.ToInt32(DateTime.Now.DayOfWeek);  
            thisSat = DateAndTime.DateAdd("d", 6 - dayOweek, Today);
            TimeSpan ts = thisSat - fixedDate;
            int DaysApart = ts.Days;

            if (DaysApart % 14 == 0)  
            {
                if (Today == thisSat)
                {
                    MenuSat = thisSat;
                }
                else
                {
                    MenuSat = DateAndTime.DateAdd("d", 6 + DateAndTime.Weekday(thisSat, z1), thisSat);
                }
            }
            else  
            {
                MenuSat = DateAndTime.DateAdd("d", 13 + DateAndTime.Weekday(thisSat, z1), thisSat);
            }

            int WeekCount = 0;

            do
            {
                ListItem li = new ListItem();
                li.Value = MenuSat.ToString("MM/dd/yyyy").ToString(); 
                li.Text = MenuSat.AddDays(-13).ToString("MM/dd/yyyy") + " - " + MenuSat.ToString("MM/dd/yyyy");
                pp_select_array.Add(li.Text);
                pp_select_date.Items.Add(new ListItem(li.Text, li.Value));
                WeekCount++;
                MenuSat = DateAndTime.DateAdd("d", -14, MenuSat);
            } while (WeekCount < 53);
        }


aspx page:

<asp:ListBox ID="pp_select_date" runat="server" Height="150px" Width="200px" DataTextField="showdate" DataValueField="enddate">
    <asp:ListItem Text="Select Date:" Value="" Selected="True"></asp:ListItem>
</asp:ListBox>

please use [code]

[/code] tags when posting code.

What is DateAndTime in your code?

Sorry... DateAndTime is a vb.net method: Microsoft.VisualBasic.DateAndTime
I'm using for the DateAdd function

Can you just clarify what you want before i make any suggestions:
In your code you have pay_period_begin; Am i correct in thinking that your working code will show one years worth of pay periods running up to that date?
Also, in your code you work out what this saterday will be and make the first item in the list the next pay period starting after that date. Did you intend for the first item to be the pay period that hasn't yet started or were you aiming to have the current pay period as the first item?

Yes & No. The hard-coded date is just a starting place to calculate from. I need to display the date ranges like this:
8/28/2010 - 9/10/2010
8/14/2010 - 8/27/2010
... for the past year. The top date range should be the "current" pay period, but next week, the list should look like this:
9/11/2010 - 9/24/2010
8/28/2010 - 9/10/2010
8/14/2010 - 8/27/2010
...

You are overthinking the problem but you are on the right tracks.
You had the right idea using the modulus of DaysApart to figure out how far into a pay period a day is, but instead of finding out if this saterday is the first or second sat of a pay period, you can use the same mod calculation to find out how far into the current pay period the date is and work back from that to get the start of the period:

static void Main(string[] args)
    {
        DateTime fixedDate = new DateTime(2008, 09, 27); // Some starting Saturday Pay Period //
        DateTime Today = DateTime.Now;

        //calculate how far into current period date is
        TimeSpan timeSinceFixedDate = Today - fixedDate;
        double dayOfCurrentPeriod = timeSinceFixedDate.Days % 14;

        //calculate start of current pay period
        DateTime startOfCurrentPeriod = Today.AddDays(dayOfCurrentPeriod * -1);

        //for every two week period starting now and ending one year ago, output period dates
        for (DateTime dt = startOfCurrentPeriod; dt > startOfCurrentPeriod.AddYears(-1); dt = dt.AddDays(-14))
        {
            Console.WriteLine(dt.ToShortDateString() + "-" + dt.AddDays(13).ToShortDateString());
        }

        Console.ReadKey();

    }

Be sure to read through and make sure you understand what each part is doing. This is a console app so you'll have to adapt it to your own needs. Let me know if you need me to go over anything :)

THANK YOU SO MUCH Ryshad !!!! I guess I was seriously over-thinking the whole date calculations!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.