Tuesday, June 29, 2010

How to find end date of any month in any year - C#

We can use an Enum to find end dates of any month in given year:
public partial class Test : Page
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        string[] months = { "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" };
        for (int y = 1998; y < 2010; y++)
        {
            this.Response.Write(y + " = ");
            foreach (string m in months)
            {
                this.Response.Write(m + ": " + Month.GetLastDate(m, y));
                this.Response.Write(" - ");
            }
            this.Response.Write("<hr />");
        }
    }
    protected override void OnInit(EventArgs e)
    {
    }
}
public class Month
{
    public enum MonthEndDate
    {
        Jan = 31,
        Feb = 28,
        Mar = 31,
        Apr = 30,
        May = 31,
        Jun = 30,
        Jul = 31,
        Aug = 31,
        Sep = 30,
        Oct = 31,
        Nov = 30,
        Dec = 31
    };
    public static int GetLastDate(string month, int year)
    {
        MonthEndDate endDate = (MonthEndDate)Enum.Parse(typeof(MonthEndDate), month);
        int enddate = (int)endDate;
        if (endDate == MonthEndDate.Feb)
            if (year % 4 == 0) enddate++;
        return enddate;
    }
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...