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.

Saturday, April 6, 2013

Convert Number Into Word

In most of cases we need to show the number (currency) in words as per Indian Standard form. So, I have created a small program that may be found to be helpful to us. I have created new console application that shows how the number can be shown in words based on Indian Standard form. Following screen shot itself shows how program result will it be.

Here is complete code...

File Name : Program.cs

using System;

namespace NumberToWordDemo {
    public class Program {
        public static void Main(String[] args)  {
            System.Console.WriteLine("Actual Number : " + int.MaxValue + "\nNumber In Word : " + NumberDescriptor.NumberToWord(int.MaxValue));
            System.Console.ReadKey();
        }
    }

    public class NumberDescriptor
    {
        public static String UnitToWord(int x)
        {
            String strNumber = "Zero";
            switch(x)
            {
                case 0:
                    strNumber = "Zero";
                break;
                case 1:
                    strNumber = "One";
                break;
                case 2:
                    strNumber = "Two";
                break;
                case 3:
                    strNumber = "Three";
                break;
                case 4:
                    strNumber = "Four";
                break;
                case 5:
                    strNumber = "Five";
                break;
                case 6:
                    strNumber = "Six";
                break;
                case 7:
                    strNumber = "Seven";
                break;
                case 8:
                    strNumber = "Eight";
                break;
                case 9:
                    strNumber = "Nine";
                break;
            }
            return strNumber;
        }

        public static String TensToWord(int x)
        {
            int tensValue = x/10;
            int unitValue = x%10;
            String strNumber = "";

            if(x > 0 && x < 10)
            {
                strNumber = UnitToWord(unitValue);
            }
            else if(x >= 10 && x <= 19)
            {
                switch(x)
                {
                    case 10:
                        strNumber = "Ten";
                    break;
                    case 11:
                        strNumber = "Eleven";
                    break;
                    case 12:
                        strNumber = "Twelve";
                    break;
                    case 13:
                        strNumber = "Thirteen";
                    break;
                    case 14:
                        strNumber = "Fourteen";
                    break;
                    case 15:
                        strNumber = "Fifteen";
                    break;
                    case 16:
                        strNumber = "Sixteen";
                    break;
                    case 17:
                        strNumber = "Seventeen";
                    break;
                    case 18:
                        strNumber = "Eighteen";
                    break;
                    case 19:
                        strNumber = "Nineteen";
                    break;
                }
            }
            else if(x >= 20 && x <= 99)
            {
                switch(tensValue)
                {
                    case 2:
                        strNumber = "Twenty";
                    break;
                    case 3:
                        strNumber = "Thirty";
                    break;
                    case 4:
                        strNumber = "Fourty";
                    break;
                    case 5:
                        strNumber = "Fifty";
                    break;
                    case 6:
                        strNumber = "Sixty";
                    break;
                    case 7:
                        strNumber = "Seventy";
                    break;
                    case 8:
                        strNumber = "Eighty";
                    break;
                    case 9:
                        strNumber = "Ninety";
                    break;
                }

                if(unitValue > 0)
                {
                    strNumber = strNumber + " " + UnitToWord(unitValue);
                }
            }

            return strNumber;
        }

        public static String NumberToWord(int x)
        {
            int offset = 1000000000;
            int index = 0;
            String strNumber = "";

            while(offset >= 1000)
            {
                if(x / offset > 0)
                {
                    strNumber += TensToWord(x / offset) + " " + Position(index) + " ";
                }

                index++;
                x = x % offset;
                offset = offset / 100;
            }

            if(x / 100 > 0)
            {
                strNumber += TensToWord(x / 100) + " " + Position(index) + " ";
                x = x % 100;
            }

            if(x > 0)
            {
                strNumber += TensToWord(x);
            }

            return strNumber.Trim();
        }

        private static String Position(int index)
        {
            String strIndex = "";

            switch(index)
            {
                case 0:
                    strIndex = "Billion";
                break;
                case 1:
                    strIndex = "Crore";
                break;
                case 2:
                    strIndex = "Lac";
                break;
                case 3:
                    strIndex = "Thousand";
                break;
                case 4:
                    strIndex = "Hundred";
                break;
            }

            return strIndex;
        }
    }
}



Above code can be modified or can be used as it is for free.

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...