This method throws an ArgumentOutOfRangeException when attempting to format a DateTime whose value is outside the valid range for the current culture's calender. For example, both the JapaneseCalendar and UmAlQuraCalender objects cannot represent every valid value of a DateTime. You can find the range of supported values from the Calendar.MinSupportedDateTime and Calendar.MaxSupportedDateTime properties.
For example, an ArgumentOutOfRangeException will be thrown when using the the JapaneseCalendar to format a date that occurs before September 8th, 1868.
[C#]
using System;
using System.Globalization;
namespace Samples
{
class Program
{
static void Main(string[] args)
{
CultureInfo culture = new CultureInfo("ja-JP");
culture.DateTimeFormat.Calendar = new JapaneseCalendar();
DateTime date = new DateTime(1868, 9, 7);
try
{
date.ToString(culture);
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
The above outputs the following:
Specified time is not supported in this calendar. It should be between 09/08/186
8 00:00:00 (Gregorian date) and 12/31/9999 23:59:59 (Gregorian date), inclusive.
Parameter name: time
This can also occur without explicitly specifying the
JapaneseCalendar when the current user's calender is set to the Wareki calendar in Regional and Language Options. To set the calendar to this, do the following in Windows Vista and Windows Server 2003:
- Choose Start -> Control Panel -> Regional and Language Options
- From the Current format drop down, choose Japanese (Japan)
- Click Customize this format and select the Date tab
- Under Calendar type, choose 和暦
- Click OK and then OK again
The following method attempts to call DateTime.ToString() on a DateTime instance with the value September 7th, 1868. This code sample expects the above steps to have been done.
[C#]
using System;
using System.Globalization;
namespace Samples
{
class Program
{
static void Main(string[] args)
{
DateTime date = new DateTime(1868, 9, 7);
try
{
date.ToString();
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
The above outputs the following:
Specified time is not supported in this calendar. It should be between 09/08/186
8 00:00:00 (Gregorian date) and 12/31/9999 23:59:59 (Gregorian date), inclusive.
Parameter name: time