Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Use of DATE Jargon in C#
By Prasad H.

Use of Date Jargaon in C#.The following program will use some functions of DateTime class.
This is a console based program which shows current months calendar and also provide option
for viewing next month,year,previous month,years calendar too.


using System;

class MyDate
{
  private int Year;
  private int Month;
  private static int Day=1;


  MyDate()
  {
    DateTime today=DateTime.Now;
    this.Year=today.Year;
    this.Month=today.Month;
  }

  public static void Main()
  {
    MyDate oCalendar=new MyDate();
    oCalendar.ShowCalendar();
    oCalendar.ShowMenu();
  }

  public String GetMonthName(int Month,int MonthFormat)
  {
    //Delcaring MonthNames in the Array
    String[] MonthNames={"January","February","March","April","May","June","July","August","September","October","November","December"};
    switch(MonthFormat)
    {
      case 0:
        return MonthNames[Month-1].Substring(0,3);
      case 1:
        return MonthNames[Month-1];
      default:
        return "Invalid Month Index";
    }
  }

  public void ShowMenu()
  {
    bool CanExit=false;
    String strChoice;
    do
    {
      Console.WriteLine("Calendar Menu");
      Console.WriteLine("=================");
      Console.WriteLine("1. [> ] Next Month");
      Console.WriteLine("2. [< ] Prev Month");
      Console.WriteLine("3. [>>] Next Year");
      Console.WriteLine("4. [<<] Prev Year");
      Console.WriteLine("5. [x ] Exit");
      Console.Write("Enter Your Choice: ");
      strChoice=Console.ReadLine();
      switch(strChoice.ToLower())
      {
        case ">":
          if(Month==12)
          {
            Year+=1;
            Month=1;
          }
          else
          {
            Month+=1;
          }
          break;
        case "<":
          if(Month==1)
          {
            Month=12;
            Year-=1;
          }
          else
          {
            Month-=1;
          }
          break;
        case ">>":
          Year+=1;
          break;
        case "<<":
          Year-=1;
          break;
        case "x":
          CanExit=true;
          break;
        default:
          Console.WriteLine("Invalid Choice");
      }
      if(!CanExit)
        ShowCalendar();
    }
    while(!CanExit);
  }

  public void ShowCalendar()
  {
    int[] arrDays=new int[35];
    int DayIndex;
    DateTime objDate=new DateTime(Year,Month,Day);

    //Get the In weekday the first of month falls
    int StartDayIndex=objDate.DayOfWeek;

    Console.WriteLine("{0} - {1}",GetMonthName(Month,1),Year);
    for(int i=0;i < 35;i++)
    {
      DayIndex=(StartDayIndex+i)%35;
      if(i < DateTime.DaysInMonth(Year,Month))
      {
        arrDays[DayIndex]=(i+1);
      }
      else
      {
        arrDays[DayIndex]=0;
      }
    }
    Console.WriteLine(new String('*',64));
    Console.WriteLine("\tSu\tMo\tTu\tWe\tTh\tFr\tSa");
    Console.WriteLine(new String('*',64));

    for(int i=0;i < 5;i++)
    {
      Console.Write("\t");
      for(int j=0;j < 7;j++)
      {
          if(arrDays[7*i+j] > 0)
          {
            Console.Write("{0,-2}\t",arrDays[7*i+j]);
          }
          else
          {
            Console.Write("{0,-2}\t"," ");
          }
      }
      Console.WriteLine();
    }
    Console.WriteLine(new String('*',64));
  }

  /*
    Other Useful functions in DateTime Class
    1.  IsLeapYear() Tells whether the Year is Leap or not
    2.  AddMonths(),AddDays(),AddYears(),Format()
  */

}