Add to year, month, week, day, hour, minute, second and millisecton : Calendar « Date Time « C# / C Sharp






Add to year, month, week, day, hour, minute, second and millisecton

 


using System;
using System.Globalization;


public class MainClass{

   public static void Main()  {
      DateTime myDT = new DateTime( 2010, 4, 13, new GregorianCalendar() );

      Calendar myCal = CultureInfo.InvariantCulture.Calendar;

      myDT = myCal.AddYears( myDT, 5 );
      myDT = myCal.AddMonths( myDT, 5 );
      myDT = myCal.AddWeeks( myDT, 5 );
      myDT = myCal.AddDays( myDT, 5 );
      myDT = myCal.AddHours( myDT, 5 );
      myDT = myCal.AddMinutes( myDT, 5 );
      myDT = myCal.AddSeconds( myDT, 5 );
      myDT = myCal.AddMilliseconds( myDT, 5 );

      DisplayValues( myCal, myDT );

   }

   public static void DisplayValues( Calendar myCal, DateTime myDT )  {
      Console.WriteLine( "Era:          {0}", myCal.GetEra( myDT ) );
      Console.WriteLine( "Year:         {0}", myCal.GetYear( myDT ) );
      Console.WriteLine( "Month:        {0}", myCal.GetMonth( myDT ) );
      Console.WriteLine( "DayOfYear:    {0}", myCal.GetDayOfYear( myDT ) );
      Console.WriteLine( "DayOfMonth:   {0}", myCal.GetDayOfMonth( myDT ) );
      Console.WriteLine( "DayOfWeek:    {0}", myCal.GetDayOfWeek( myDT ) );
      Console.WriteLine( "Hour:         {0}", myCal.GetHour( myDT ) );
      Console.WriteLine( "Minute:       {0}", myCal.GetMinute( myDT ) );
      Console.WriteLine( "Second:       {0}", myCal.GetSecond( myDT ) );
      Console.WriteLine( "Milliseconds: {0}", myCal.GetMilliseconds( myDT ) );
   }

}

   
  








Related examples in the same category

1.Calendar ToDateTime is not culture aware
2.ToString(): make it culture aware
3.Two Digit Year Max
4.To Four Digit Year
5.Create date of 1/13/2009 using Hijri calendar and get Day property from DateTime
6.Calendar represents time in divisions, such as weeks, months, and years.
7.DateTime(Int32, Int32, Int32, Calendar): Create a DateTime structure to the specified year, month, and day for the specified calendar.
8.Using the Hijri Calendar
9.Using the Persian Calendar