thewrongadvice
thewrongadvice

Reputation: 35

Trying to write my own date program, need a check for max date for specified month in C#

I'm trying to write my own Date program in C#, the problem I've having is that when the user inputs a date for example 2-31, the program allows it. I want to create a condition where I can match which month is entered and then from that see if the day is available in that month. I'm using this code below but it's throwing me the exception for any day such as October 10 which should be right. If I comment this out the date works but it won't check to match the month.

public int Day
    {
        get
        {
            return day;
        } 
        private set 
        {
            //int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 
            //                 31, 31, 30, 31, 30, 31 };

            //// check if day in range for month
            //if (value > 0 && value <= daysPerMonth[Month])
            //    day = value;

            //else // day is invalid
            //    throw new ArgumentOutOfRangeException(
            //       "Day", value, "Day out of range for current month/year");

            if (value > 0 && value <= 31) 
                day = value;
            else 
                throw new ArgumentOutOfRangeException("Day", value, "Day must be 1-31");

        } 
    } 

Upvotes: 2

Views: 181

Answers (2)

Bhavik
Bhavik

Reputation: 4904

If it had to be done using DateTime it can be done using

DateTime.DaysInMonth(Year, Month);  

Refrence

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149050

You'll have to know what month the user is selecting as well as the year (to correctly handle leap years).

It would have to be something like this:

public int Day
{
    get
    {
        return day;
    } 
    private set 
    {
        var endOfMonth = new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
        if (value > 0 && value <= endOfMonth.Day) 
            day = value;
        else 
        {
            var message = string.Format("Day must be between {0} and {1}", 1 endOfMonth.Day);
            throw new ArgumentOutOfRangeException("Day", value, message);
        }
    } 
} 

Where year and month are other fields in your class. If you really want to do this without any reference to the DateTime class, I recommend extracting this logic out into a static class which can do the math without having to re-code it any time you want to get the last day of the month.

public static class DateHelper
{
    private int[] daysInMonth = new[] { 31, 28, 31, 30, 31, 30, 
                                        31, 31, 30, 31, 30, 31 };

    public static bool IsLeapYear(int year)
    {
        // TODO: taken from wikipedia, can be improved
        if (year % 400 == 0)
            return true;
        else if (year % 100 == 0)
            return false;
        else if (year % 4 == 0)
            return true;
        return false;
    }

    public static bool GetDaysInMonth(int year, int month)
    {
        // TODO: check for valid ranges
        var days = daysInMonth[month - 1];
        if (month == 2 && IsLeapYear(year))
            days++;
        return days;
    }
}

Then you can use it like this:

public int Day
{
    get
    {
        return day;
    } 
    private set 
    {
        var endOfMonth = DateHelper.GetDaysInMonth(year, month);
        if (value > 0 && value <= endOfMonth) 
            day = value;
        else 
        {
            var message = string.Format("Day must be between {0} and {1}", 1 endOfMonth);
            throw new ArgumentOutOfRangeException("Day", value, message);
        }
    } 
} 

Upvotes: 1

Related Questions