Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

PersianCalendar.ToDateTime Method (Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

Returns a DateTime object that is set to the specified date, time, and era.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
public override DateTime ToDateTime(
	int year,
	int month,
	int day,
	int hour,
	int minute,
	int second,
	int millisecond,
	int era
)

Parameters

year
Type: System.Int32
An integer from 1 through 9378 that represents the year.
month
Type: System.Int32
An integer from 1 through 12 that represents the month.
day
Type: System.Int32
An integer from 1 through 31 that represents the day.
hour
Type: System.Int32
An integer from 0 through 23 that represents the hour.
minute
Type: System.Int32
An integer from 0 through 59 that represents the minute.
second
Type: System.Int32
An integer from 0 through 59 that represents the second.
millisecond
Type: System.Int32
An integer from 0 through 999 that represents the millisecond.
era
Type: System.Int32
An integer from 0 through 1 that represents the era.

Return Value

Type: System.DateTime
A DateTime object that is set to the specified date and time in the current era.
ExceptionCondition
ArgumentOutOfRangeException

year, month, day, hour, minute, second, millisecond, or era is outside the range supported by this calendar.

The ToDateTime method is useful because it can convert any date in the current calendar to a Gregorian calendar date. The Gregorian date can subsequently be used, for example, to compare dates in different calendars or create an equivalent date in a particular calendar.

The following example uses the ToDateTime method.


using System;
using System.Globalization;

class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock) 
    {
		//--------------------------------------------------------------------------------
		// Get today's date.
		//--------------------------------------------------------------------------------
      outputBlock.Text += "\n................. Today ...........................\n";
      PersianCalendar jc = new PersianCalendar();
      DateTime thisDate = DateTime.Now;

		// Display the current date using the Gregorian and Persian calendars.
	  outputBlock.Text += "Today is:\n";
      outputBlock.Text += String.Format("   {0:dddd}, {0} in the Gregorian calendar.\n", thisDate);
      outputBlock.Text += String.Format("   {0}, {1}/{2}/{3} {4}:{5}:{6} in the Persian calendar.\n", 
                                        jc.GetDayOfWeek(thisDate), 
                                        jc.GetMonth(thisDate), 
                                        jc.GetDayOfMonth(thisDate), 
                                        jc.GetYear(thisDate), 
                                        jc.GetHour(thisDate), 
                                        jc.GetMinute(thisDate), 
                                        jc.GetSecond(thisDate));
		//--------------------------------------------------------------------------------
		// Fields
		//--------------------------------------------------------------------------------
      outputBlock.Text += "\n............... Fields .............................\n";
      outputBlock.Text += String.Format("PersianEra = {0}\n", PersianCalendar.PersianEra);
		//--------------------------------------------------------------------------------
		// Properties
		//--------------------------------------------------------------------------------
      outputBlock.Text += "\n............... Properties .........................\n";
      outputBlock.Text += "Eras:";
      foreach (int era in jc.Eras)
      {
         outputBlock.Text += String.Format(" era = {0}\n", era);
      }
		//--------------------------------------------------------------------------------
      outputBlock.Text += "\nGregorian Date Range Supported by the Persian Calendar:\n";
      outputBlock.Text += String.Format("   From {0:G}\n", jc.MinSupportedDateTime);
      outputBlock.Text += String.Format("   To {0:G}\n", jc.MaxSupportedDateTime);
		//--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("\nTwoDigitYearMax = {0}\n", jc.TwoDigitYearMax);
		//--------------------------------------------------------------------------------
		// Methods
		//--------------------------------------------------------------------------------
      outputBlock.Text += "\n............ Selected Methods .......................\n";

		//--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("GetDayOfYear: day = {0}\n", jc.GetDayOfYear(thisDate));
		//--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("GetDaysInMonth: days = {0}\n", 
                                        jc.GetDaysInMonth( thisDate.Year, thisDate.Month, 
                                        PersianCalendar.PersianEra));
		//--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("GetDaysInYear: days = {0}\n", 
                        jc.GetDaysInYear(thisDate.Year, PersianCalendar.PersianEra));
		//--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("GetLeapMonth: leap month (if any) = {0}\n", 
                        jc.GetLeapMonth(thisDate.Year, PersianCalendar.PersianEra));
		//-------------------------------------------------------------
      outputBlock.Text += String.Format("GetMonthsInYear: months in a year = {0}\n", 
                        jc.GetMonthsInYear(thisDate.Year, PersianCalendar.PersianEra));
		//--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("IsLeapDay: This is a leap day = {0}\n", 
                        jc.IsLeapDay(thisDate.Year, thisDate.Month, thisDate.Day, 
                        PersianCalendar.PersianEra));
		//--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("IsLeapMonth: This is a leap month = {0}\n", 
                        jc.IsLeapMonth(thisDate.Year, thisDate.Month, 
                        PersianCalendar.PersianEra));
		//--------------------------------------------------------------------------------
      outputBlock.Text += String.Format("IsLeapYear: 1370 is a leap year = {0}\n", 
                        jc.IsLeapYear(1370, PersianCalendar.PersianEra));
		//--------------------------------------------------------------------------------

		// Get the 4-digit year for a year whose last two digits are 99. The 4-digit year 
		// depends on the current value of the TwoDigitYearMax property.

      outputBlock.Text += "ToFourDigitYear:\n";
      outputBlock.Text += String.Format("  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}\n", 
                         jc.TwoDigitYearMax, jc.ToFourDigitYear(99));
      jc.TwoDigitYearMax = thisDate.Year;
      outputBlock.Text += String.Format("  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}\n", 
                        jc.TwoDigitYearMax, jc.ToFourDigitYear(99));
    }
}
/*
The example produces the following results:

................. Today ...........................

Today is Wednesday, 6/14/2006 11:31:13 AM

............... Fields .............................

PersianEra = 1

............... Properties .........................

Eras: era = 1
MaxSupportedDateTime = 12/31/9999 11:59:59 PM
MinSupportedDateTime = 3/21/0622 12:00:00 AM
TwoDigitYearMax = 1410

................ Methods ...........................

AddMonths: thisDate + 6 months = 12/15/2006 11:31:13 AM
           thisDate - 6 months = 12/15/2005 11:31:13 AM
AddYears:  thisDate + 5 years =  6/14/2011 11:31:13 AM
           thisDate - 3 years =  6/14/2003 11:31:13 AM
GetDayOfMonth: month = 24
GetDayOfWeek: day = Wednesday
GetDayOfYear: day = 86
GetDaysInMonth: days = 31
GetDaysInYear: days = 366
GetEra: era = 1
GetLeapMonth: leap month (if any) = 0
GetMonth: month = 3
GetMonthsInYear: months in a year = 12
GetYear: year = 1385
IsLeapDay: This is a leap day = False
IsLeapMonth: This is a leap month = False
IsLeapYear: 1370 is a leap year = True
ToDateTime:
  Gregorian calendar: 6/10/2006 8:30:15 PM
  Persian calendar:   3/20/1385 8:30:15 PM
ToFourDigitYear:
  If TwoDigitYearMax = 1410, ToFourDigitYear(99) = 1399
  If TwoDigitYearMax = 2006, ToFourDigitYear(99) = 1999

*/


Silverlight

Supported in: 5

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.