JapaneseCalendar.ToDateTime Method

Definition

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

C#
public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era);

Parameters

year
Int32

An integer that represents the year.

month
Int32

An integer from 1 to 12 that represents the month.

day
Int32

An integer from 1 to 31 that represents the day.

hour
Int32

An integer from 0 to 23 that represents the hour.

minute
Int32

An integer from 0 to 59 that represents the minute.

second
Int32

An integer from 0 to 59 that represents the second.

millisecond
Int32

An integer from 0 to 999 that represents the millisecond.

era
Int32

An integer that represents the era.

Returns

The DateTime that is set to the specified date and time in the current era.

Exceptions

year is outside the range supported by the calendar.

-or-

month is outside the range supported by the calendar.

-or-

day is outside the range supported by the calendar.

-or-

hour is less than zero or greater than 23.

-or-

minute is less than zero or greater than 59.

-or-

second is less than zero or greater than 59.

-or-

millisecond is less than zero or greater than 999.

-or-

era is outside the range supported by the calendar.

Examples

The following example instantiates two dates. The first is always the first day of the second year in the current era, while the second identifies a specific day in the Taisho era. The output from the example was produced with the Heisei era as the current era.

C#
using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        var cal = new JapaneseCalendar();
        var jaJp = new CultureInfo("ja-JP");
        jaJp.DateTimeFormat.Calendar = cal;
        var date1 = cal.ToDateTime(2,1,1,0,0,0,0,JapaneseCalendar.CurrentEra);
        Console.WriteLine($"Japanese calendar date: {date1.ToString("D", jaJp)}, " +
                          $"Gregorian calendar date: {date1.ToString("D", CultureInfo.InvariantCulture)}");

        var date2 = cal.ToDateTime(6,11,7,0,0,0,0,GetEraIndex("大正"));
        Console.WriteLine($"Japanese calendar date: {date2.ToString("D", jaJp)}, " +
                          $"Gregorian calendar date: {date2.ToString("D", CultureInfo.InvariantCulture)}");

        int GetEraIndex(string eraName)
        {
           foreach (var ctr in cal.Eras)
              if (jaJp.DateTimeFormat.GetEraName(ctr) == eraName)
                 return ctr;

           return 0; 
        }
    }
}
// The example displays the following output:
//   Japanese calendar date: 平成2年1月1日, Gregorian calendar date: Monday, 01 January 1990
//   Japanese calendar date: 大正6年11月7日, Gregorian calendar date: Wednesday, 07 November 1917

Remarks

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.

Because the JapaneseCalendar supports multiple eras based on the reign of the emperor, you should always call this method and explicitly specify an era to avoid an unintended date and to make the intent of your code clear. The example shows how to instantiate a date that is always in the current era and one that belongs to a specified era.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also