Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:07:47+00:00 2026-06-18T01:07:47+00:00

I have a dropdownlist called ddlPayrollDate in my asp.net web site. I want to

  • 0

I have a dropdownlist called ddlPayrollDate in my asp.net web site. I want to insert/bound dates 2 and 17 of last 3 months, including the current month. To consider when a year is changed, I had to write multiple conditions in my C# code which is given below is my code:

     ddlPayrollDate.Items.Insert(1, String.Format("{0:MMMM dd, yyyy}", new DateTime (DateTime.UtcNow.Year, DateTime.UtcNow.Month, 17)));
                ddlPayrollDate.Items.Insert(2, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 2)));
                if (DateTime.UtcNow.Month != 1)
                    ddlPayrollDate.Items.Insert(3, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 1, 17)));
                else
                    ddlPayrollDate.Items.Insert(3, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 12, 17)));

                if (DateTime.UtcNow.Month != 1)
                    ddlPayrollDate.Items.Insert(4, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 1, 2)));
                else
                    ddlPayrollDate.Items.Insert(4, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 12, 2)));

                if (DateTime.UtcNow.Month != 1 && DateTime.UtcNow.Month != 2)
                    ddlPayrollDate.Items.Insert(5, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 2, 17)));
                else if (DateTime.UtcNow.Month == 1)
                    ddlPayrollDate.Items.Insert(5, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 11, 17)));
                else
                    ddlPayrollDate.Items.Insert(5, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 12, 17)));

                if (DateTime.UtcNow.Month != 1 && DateTime.UtcNow.Month != 2)
                    ddlPayrollDate.Items.Insert(6, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 2, 2)));
                else if (DateTime.UtcNow.Month == 1)
                    ddlPayrollDate.Items.Insert(6, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 11, 2)));
                else
                    ddlPayrollDate.Items.Insert(6, String.Format("{0:MMMM dd, yyyy}", new DateTime(DateTime.UtcNow.Year - 1, 12, 2)));

Is there a way I can optimize this code without having these many conditions by using any datetime functions. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:07:48+00:00Added an answer on June 18, 2026 at 1:07 am

    It seems to me that you want a loop. Something like:

    DateTime now = DateTime.UtcNow;
    // Use the start of the month to avoid concerns around short months etc
    DateTime current = new DateTime(now.Year, now.Month, 1);
    
    for (int i = 0; i < 3; i++)
    {        
        // Add the 17th... (add 16 days to the 1st)
        ddlPayrollDate.Items.Insert(i * 2 + 1,
                                    current.AddDays(16).ToString("MMMM dd, yyyy"));
        // Add the 2nd... (add 1 day to the 1st)
        ddlPayrollDate.Items.Insert(i * 2 + 2,
                                    current.AddDays(1).ToString("MMMM dd, yyyy"));
        // Go back a month...
        current = current.AddMonths(-1);
    }
    

    This assumes that you always want the 17th and the 2nd of the current month (in UTC), whether the current day of month is before the 2nd, between the 2nd and the 17th, or after the 17th.

    Also note that you should usually use the invariant culture when specifying a custom date format; or use a standard date format with a “normal” culture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains
I have an ASP.NET dropdownlist called #ddlDateRange that contains several items. Possible values in
I have the following JavaScript to disable a dropdownlist in a ASP.NET page, which
I am using ASP.NET 3.5 I have a drop-down list called lstCountry with an
How to make a dynamic dropdownlist on asp.net? I have a dropdownlist : <asp:DropDownList
I have a DropDownList called ShippingList and when a value is selected, I want
I have a dropdownlist in aspx called ddlService . I want to added the
I have a dropdownlist. A column called id has the control dropdownlist ( I've
I have DropDownList with CascadingDropDown extender. If I want to retrieve data from the
I have a DropDownList with an SelectedIndexChanged event listener <asp:DropDownList ID=LoanOptionCombo runat=server AutoProstBack=True> Listener

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.