What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 64976 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > Find Quarter given Month name in C# ...
Niladri.Biswas

Find Quarter given Month name in C#

 Code Snippet posted by: Niladri.Biswas | Posted on: 4/6/2012 | Category: C# Codes | Views: 1001 | Status: [Member] | Points: 40 | Alert Moderator   


Sometime, we may need to get the Quarter in which the month falls. Here is my attempt in C#

private static int GetQuarter(string month)

{
return MonthQuarterSource()[month];
}

private static Dictionary<string, int> MonthQuarterSource()
{
Dictionary<string, int> dictMonthQuarter = new Dictionary<string, int>();
dictMonthQuarter.Add("January", 1);
dictMonthQuarter.Add("February", 1);
dictMonthQuarter.Add("March", 1);
dictMonthQuarter.Add("April", 2);
dictMonthQuarter.Add("May", 2);
dictMonthQuarter.Add("June", 2);
dictMonthQuarter.Add("July", 3);
dictMonthQuarter.Add("August", 3);
dictMonthQuarter.Add("September", 3);
dictMonthQuarter.Add("October", 4);
dictMonthQuarter.Add("November", 4);
dictMonthQuarter.Add("December", 4);
return dictMonthQuarter;
}


We are first creating a dictionay object that stores the Month name and the corresponding Quarter it belongs to. Then by passing the Month name as key, we obtain the Quarter as it's value as shown below

var result = GetQuarter("January");


Hope this helps.

Best Regards,
Niladri Biswas
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 8:03:50 AM