Tuesday, April 9, 2013

How to get day of a date without using date functions

In most of cases we use some date functions directly to get day of that date. But there is a logical patter that calender follows and hence it is possible to find out the day of any valid date that we know and following code sample works well in that situations. Purpose of this code is show that the days mentioned in the calender follows some common logical rules and hence it is easy to find the day of that date.

Create a new console application say "DayCalculatorDemo" then copy and paste the following code sample as follows....

FileName : Program.cs

using System;

namespace DayCalculatorDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("The day of 12 August 1986 is " + DayCalculator.GetDayFromDate(1986, 8, 12));
            Console.ReadKey();
        }
    }

    public class DayCalculator
    {
        public static String GetDayFromDate(int year, int month, int day)
        {
            String strDay = String.Empty;

            if(!IsDateValid(year, month, day))
            {
                return String.Empty;
            }

            int yearOffset = year % 28;
            int yearFactor = (yearOffset % 7) + (int)(yearOffset / 4) - (yearOffset % 4 == 0 ? 1 : 0);

            int monthFactor = 0;
            switch(month)
            {
                case 1:
                case 10:
                    monthFactor = 0;
                break;

                case 5:
                    monthFactor = 1;
                break;

                case 8:
                    monthFactor = 2;
                break;

                case 2:
                case 3:
                case 11:
                    monthFactor = 3;
                break;

                case 6:
                    monthFactor = 4;
                break;

                case 9:
                case 12:
                    monthFactor = 5;
                break;

                case 4:
                case 7:
                    monthFactor = 6;
                break;
            }

            if(IsLeapYear(year) && month > 2)
            {
                monthFactor++;
            }

            int dayFactor = day % 7;

            int dayIndex = (yearFactor + monthFactor + dayFactor + 6) % 7;

            switch(dayIndex)
            {
                case 1:
                    strDay = "Sunday";
                break;

                case 2:
                    strDay = "Monday";
                break;

                case 3:
                    strDay = "Tuesday";
                break;

                case 4:
                    strDay = "Wednesday";
                break;

                case 5:
                    strDay = "Thursday";
                break;

                case 6:
                    strDay = "Friday";
                break;

                case 0:
                    strDay = "Saturday";
                break;
            }

            return strDay;
        }

        private static bool IsDateValid(int year, int month, int day)
        {
            bool isValid = true;

            if(day > 31)
            {
                isValid = false;
            }
            else if(month > 12)
            {
                isValid = false;
            }
            else if(year <= 0 || month <= 0 || day <= 0)
            {
                isValid = false;
            }
            else if(month == 2 && (!IsLeapYear(year)) && day >= 29)
            {
                isValid = false;
            }
            else if(month == 2 && day >= 30)
            {
                isValid = false;
            }
            else if((month == 2 || month == 4 || month == 6 || month == 9 || month == 11) && (day > 30))
            {
                isValid = false;
            }

            return isValid;
        }

        private static bool IsLeapYear(int year)
        {
            return (year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0);
        }
    }
}


above code is free to use and modify. You can suggest any changes in the above code. It can work for at least 400 years perfectly.

No comments:

Post a Comment

Age Calculator - Calculating difference between dates

Dear friends in most of the cases you need to work on dates. I had been stuck while calculating exact age of a person or total experience i...