• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

determine year

I've got a issue where I'm trying to figure out how to get the year from a ddl selection. the code below works great in the middle of the year but what if it is around the end or beginning of a new year. I'm not quite sure how to handle that with what i have below.

protected void getMonthVal()
        {
            //called with ddlMonth_SelectedIndexChanged

            //current month september 2012
            string previousMonth = string.Empty;
            string y = DateTime.Now.ToString("yyyy");

            //previous month
            if (ddlMonth.SelectedValue == "1")
            {
                previousMonth = DateTime.Now.AddMonths(-1).ToString("MMMM");
                lblMonthSelected.Text = previousMonth.ToString() + " " + y.ToString();
            }

            //current month
            if (ddlMonth.SelectedValue == "2")
            {
                lblMonthSelected.Text = getMonthName(DateTime.Now) + " " + y.ToString();
            }

            //next month
            if (ddlMonth.SelectedValue == "3")
            {
                previousMonth = DateTime.Now.AddMonths(+1).ToString("MMMM");
                lblMonthSelected.Text = previousMonth.ToString() + " " + y.ToString();
            }

            //advance 2 months
            if (ddlMonth.SelectedValue == "4")
            {
                previousMonth = DateTime.Now.AddMonths(+2).ToString("MMMM");
                lblMonthSelected.Text = previousMonth.ToString() + " " + y.ToString();
            }
        }

Open in new window


say if current month is Dec 2012, user selects next month - Jan 2013 or advances 2 months Feb 2013.

another situation is if the user is in Jan 2013 and selects previous month Dec 2012.
0
fwstealer
Asked:
fwstealer
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
ToddBeaulieuCommented:
Hello,

You are extracting the year from "today", but the month from the computed period. Just compute the new date as a DATETIME and then extract everything you want from that.

            DateTime startingDate = DateTime.Parse("01/01/2013");
            DateTime newDate;

            newDate = startingDate.AddMonths(-1);
            Console.WriteLine("Previous month: {0}", newDate.ToString("MMMM yyyy"));
       

            //next month
            newDate = startingDate.AddMonths(+1);
            Console.WriteLine("Previous month: {0}", newDate.ToString("MMMM yyyy"));
0
 
LIONKINGCommented:
What is it that you want to achieve? I still don't quite get it, sorry...

You select a month/year and you want to be able to go back or forward X amount of months, is that it?
0
 
fwstealerAuthor Commented:
right now it defaults to current month 2.

if user selects previous month 1 then i need to account for the proper month/yr
next month - same account for proper month/yr
advance 2 months - same account for proper month/yr

all based on current month/yr
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
fwstealerAuthor Commented:
so would it be something like:

protected void getMonthVal()
        {
            //called with ddlMonth_SelectedIndexChanged
            DateTime startingDate = DateTime.Parse(DateTime.Now.ToString());
            DateTime newDate;

            if (ddlMonth.SelectedValue == "1")
            {
                newDate = startingDate.AddMonths(-1);
                lblMonthSelected.Text = newDate.ToString("MMMM yyyy");
            }

            //current month
            if (ddlMonth.SelectedValue == "2")
            {
                lblMonthSelected.Text = startingDate.ToString("MMMM yyyy");
            }

            //next month
            if (ddlMonth.SelectedValue == "3")
            {
                newDate = startingDate.AddMonths(+1);
                lblMonthSelected.Text = newDate.ToString("MMMM yyyy");
            }

            //advance 2 months
            if (ddlMonth.SelectedValue == "4")
            {
                newDate = startingDate.AddMonths(+2);
                lblMonthSelected.Text = newDate.ToString("MMMM yyyy");
            }
}

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just do...
            // previous month
            if (ddlMonth.SelectedValue == "1")
            {
                lblMonthSelected.Text = DateTime.Now.AddMonths(-1).ToString("MMMM yyyy");
            }

           //current month
            if (ddlMonth.SelectedValue == "2")
            {
                lblMonthSelected.Text = DateTime.Now.ToString("MMMM yyyy");
            }

            //next month
            if (ddlMonth.SelectedValue == "3")
            {
                lblMonthSelected.Text = DateTime.Now.AddMonths(1).ToString("MMMM yyyy");
            }

            //advance 2 months
            if (ddlMonth.SelectedValue == "4")
            {
                lblMonthSelected.Text = DateTime.Now.AddMonths(2).ToString("MMMM yyyy");
            }

Open in new window

0
 
ToddBeaulieuCommented:
I wrote my example so it can executed with any start date, rather than the current. That way you can prove that it works correctly by testing boundary dates. It's the same solution as was just proposed, though.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Agreed Todd...my post has essentially the same approach as yours, and is just a variation.

Sorry bout that...  =)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now