Calendar.ToDateTime Method

Definition

When overridden in a derived class, returns a DateTime that is set to the specified date and time.

Overloads

ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

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

ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

When overridden in a derived class, returns a DateTime that is set to the specified date and time in the specified era.

ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

Source:
Calendar.cs
Source:
Calendar.cs
Source:
Calendar.cs

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

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

Parameters

year
Int32

An integer that represents the year.

month
Int32

A positive integer that represents the month.

day
Int32

A positive integer 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.

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.

Remarks

This method returns a date and time based on the current era of a particular calendar. For the JapaneseCalendar and JapaneseLunisolarCalendar, which support multiple eras based on the reign of the emperor, calling this method can produce an unintended date when an era changes. When instantiating a date using either of these calendars, we recommend that you call the JapaneseCalendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) or JapaneseLunisolarCalendar.ToDateTime(Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32) method and explicitly specify an era.

See also

Applies to

.NET 9 and other versions
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
.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.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

Source:
Calendar.cs
Source:
Calendar.cs
Source:
Calendar.cs

When overridden in a derived class, returns a DateTime that is set to the specified date and time in the specified era.

C#
public abstract 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

A positive integer that represents the month.

day
Int32

A positive integer 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.

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.

Notes to Implementers

The derived class must support CurrentEra when it is passed as the era parameter. One way to support CurrentEra is by replacing it with the value stored in the first element of the Eras property array, which is the value for the current era of the calendar.

See also

Applies to

.NET 9 and other versions
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
.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.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0