Edit

Share via


DateTime Constructors

Definition

Initializes a new instance of the DateTime structure.

Overloads

DateTime(Int64)

Initializes a new instance of the DateTime structure to a specified number of ticks.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond for the specified calendar.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and Coordinated Universal Time (UTC) or local time.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

DateTime(Int32, Int32, Int32, Calendar)

Initializes a new instance of the DateTime structure to the specified year, month, and day for the specified calendar.

DateTime(Int32, Int32, Int32)

Initializes a new instance of the DateTime structure to the specified year, month, and day.

DateTime(DateOnly, TimeOnly, DateTimeKind)

Initializes a new instance of the DateTime structure to the specified DateOnly and TimeOnly and respecting the specified DateTimeKind.

DateTime(Int64, DateTimeKind)

Initializes a new instance of the DateTime structure to a specified number of ticks and to Coordinated Universal Time (UTC) or local time.

DateTime(DateOnly, TimeOnly)

Initializes a new instance of the DateTime structure to the specified DateOnly and TimeOnly. The new instance will have the Unspecified kind.

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second for the specified calendar.

DateTime(Int64)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to a specified number of ticks.

C#
public DateTime(long ticks);

Parameters

ticks
Int64

A date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.

Exceptions

Examples

The following example demonstrates one of the DateTime constructors.

C#
// This example demonstrates the DateTime(Int64) constructor.
using System;
using System.Globalization;

class Sample
{
    public static void Main()
    {
// Instead of using the implicit, default "G" date and time format string, we
// use a custom format string that aligns the results and inserts leading zeroes.
    string format = "{0}) The {1} date and time is {2:MM/dd/yyyy hh:mm:ss tt}";

// Create a DateTime for the maximum date and time using ticks.
    DateTime dt1 = new DateTime(DateTime.MaxValue.Ticks);

// Create a DateTime for the minimum date and time using ticks.
    DateTime dt2 = new DateTime(DateTime.MinValue.Ticks);

// Create a custom DateTime for 7/28/1979 at 10:35:05 PM using a
// calendar based on the "en-US" culture, and ticks.
    long ticks = new DateTime(1979, 07, 28, 22, 35, 5,
    new CultureInfo("en-US", false).Calendar).Ticks;
    DateTime dt3 = new DateTime(ticks);

    Console.WriteLine(format, 1, "maximum", dt1);
    Console.WriteLine(format, 2, "minimum", dt2);
    Console.WriteLine(format, 3, "custom ", dt3);
    Console.WriteLine("\nThe custom date and time is created from {0:N0} ticks.", ticks);
    }
}
/*
This example produces the following results:

1) The maximum date and time is 12/31/9999 11:59:59 PM
2) The minimum date and time is 01/01/0001 12:00:00 AM
3) The custom  date and time is 07/28/1979 10:35:05 PM

The custom date and time is created from 624,376,461,050,000,000 ticks.

*/

Remarks

The Kind property is initialized to Unspecified.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

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.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

C#
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar);

Parameters

year
Int32

The year (1 through the number of years in calendar).

month
Int32

The month (1 through the number of months in calendar).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

microsecond
Int32

The microseconds (0 through 999).

calendar
Calendar

The calendar that is used to interpret year, month, and day.

Exceptions

calendar is null

year is not in the range supported by calendar.

-or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

-or-

microsecond is less than 0 or greater than 999.

Remarks

The allowable values for year, month, and day parameters depend on the calendar parameter. An exception is thrown if the specified date and time cannot be expressed using calendar.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

C#
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind);

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

microsecond
Int32

The microseconds (0 through 999).

kind
DateTimeKind

One of the enumeration values that indicates whether year, month, day, hour, minute, second, and millisecond specify a local time, Coordinated Universal Time (UTC), or neither.

Exceptions

year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

-or-

microsecond is less than 0 or greater than 999.

kind is not one of the DateTimeKind values.

Remarks

This constructor interprets year, month and day as a year, month and day in the Gregorian calendar. To instantiate a DateTime value by using the year, month and day in another calendar, call the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) constructor.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

C#
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar, DateTimeKind kind);

Parameters

year
Int32

The year (1 through the number of years in calendar).

month
Int32

The month (1 through the number of months in calendar).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

calendar
Calendar

The calendar that is used to interpret year, month, and day.

kind
DateTimeKind

One of the enumeration values that indicates whether year, month, day, hour, minute, second, and millisecond specify a local time, Coordinated Universal Time (UTC), or neither.

Exceptions

calendar is null.

year is not in the range supported by calendar.

-or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

kind is not one of the DateTimeKind values.

Examples

The following example calls the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) constructor twice to instantiate two DateTime values. The first call instantiates a DateTime value by using a PersianCalendar object. Because the Persian calendar cannot be designated as the default calendar for a culture, displaying a date in the Persian calendar requires individual calls to its PersianCalendar.GetMonth, PersianCalendar.GetDayOfMonth, and PersianCalendar.GetYear methods. The second call to the constructor instantiates a DateTime value by using a HijriCalendar object. The example changes the current culture to Arabic (Syria) and changes the current culture's default calendar to the Hijri calendar. Because Hijri is the current culture's default calendar, the Console.WriteLine method uses it to format the date. When the previous current culture (which is English (United States) in this case) is restored, the Console.WriteLine method uses the current culture's default Gregorian calendar to format the date.

C#
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Using the Persian Calendar:");
      PersianCalendar persian = new PersianCalendar();
      DateTime date1 = new DateTime(1389, 5, 27, 16, 32, 18, 500,
                                    persian, DateTimeKind.Local);
      Console.WriteLine("{0:M/dd/yyyy h:mm:ss.fff tt} {1}", date1, date1.Kind);
      Console.WriteLine("{0}/{1}/{2} {3}{8}{4:D2}{8}{5:D2}.{6:G3} {7}\n",
                                       persian.GetMonth(date1),
                                       persian.GetDayOfMonth(date1),
                                       persian.GetYear(date1),
                                       persian.GetHour(date1),
                                       persian.GetMinute(date1),
                                       persian.GetSecond(date1),
                                       persian.GetMilliseconds(date1),
                                       date1.Kind,
                                       DateTimeFormatInfo.CurrentInfo.TimeSeparator);

      Console.WriteLine("Using the Hijri Calendar:");
      // Get current culture so it can later be restored.
      CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

      // Define strings for use in composite formatting.
      string dFormat;
      string fmtString;
      // Define Hijri calendar.
      HijriCalendar hijri = new HijriCalendar();
      // Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
      CultureInfo current = CultureInfo.CurrentCulture;
      current.DateTimeFormat.Calendar = hijri;
      dFormat = current.DateTimeFormat.ShortDatePattern;
      // Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy") + " H:mm:ss.fff";
      fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "} {3}";
      DateTime date2 = new DateTime(1431, 9, 9, 16, 32, 18, 500,
                                    hijri, DateTimeKind.Local);
      Console.WriteLine(fmtString, current, GetCalendarName(hijri),
                        date2, date2.Kind);

      // Restore previous culture.
      Thread.CurrentThread.CurrentCulture = dftCulture;
      dFormat = DateTimeFormatInfo.CurrentInfo.ShortDatePattern +" H:mm:ss.fff";
      fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "} {3}";
      Console.WriteLine(fmtString,
                        CultureInfo.CurrentCulture,
                        GetCalendarName(CultureInfo.CurrentCulture.Calendar),
                        date2, date2.Kind);
   }

   private static string GetCalendarName(Calendar cal)
   {
      return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
   }
}
// The example displays the following output:
//    Using the Persian Calendar:
//    8/18/2010 4:32:18.500 PM Local
//    5/27/1389 16:32:18.500 Local
//
//    Using the Hijri Calendar:
//    ar-SY culture using the Hijri calendar: 09/09/1431 16:32:18.500 Local
//    en-US culture using the Gregorian calendar: 8/18/2010 16:32:18.500 Local

Remarks

The allowable values for year, month, and day parameters depend on the calendar parameter. An exception is thrown if the specified date and time cannot be expressed using calendar.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

Important

Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. For example, May 1, 2019 marked the beginning of the Reiwa era in the JapaneseCalendar and JapaneseLunisolarCalendar. Such a change of era affects all applications that use these calendars. For more information and to determine whether your applications are affected, see Handling a new era in the Japanese calendar in .NET. For information on testing your applications on Windows systems to ensure their readiness for the era change, see Prepare your application for the Japanese era change. For features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras, see Working with eras.

The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 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 2.0, 2.1

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

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

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

microsecond
Int32

The microseconds (0 through 999).

Exceptions

year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

-or-

microsecond is less than 0 or greater than 999.

Remarks

This constructor interprets year, month and day as a year, month and day in the Gregorian calendar. To instantiate a DateTime value by using the year, month and day in another calendar, call the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) constructor.

The Kind property is initialized to Unspecified.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond for the specified calendar.

C#
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar);

Parameters

year
Int32

The year (1 through the number of years in calendar).

month
Int32

The month (1 through the number of months in calendar).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

calendar
Calendar

The calendar that is used to interpret year, month, and day.

Exceptions

calendar is null.

year is not in the range supported by calendar.

-or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

Examples

The following example calls the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) constructor twice to instantiate two DateTime values. The first call instantiates a DateTime value by using a PersianCalendar object. Because the Persian calendar cannot be designated as the default calendar for a culture, displaying a date in the Persian calendar requires individual calls to its PersianCalendar.GetMonth, PersianCalendar.GetDayOfMonth, and PersianCalendar.GetYear methods. The second call to the constructor instantiates a DateTime value by using a HijriCalendar object. The example changes the current culture to Arabic (Syria) and changes the current culture's default calendar to the Hijri calendar. Because Hijri is the current culture's default calendar, the Console.WriteLine method uses it to format the date. When the previous current culture (which is English (United States) in this case) is restored, the Console.WriteLine method uses the current culture's default Gregorian calendar to format the date.

C#
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Using the Persian Calendar:");
      PersianCalendar persian = new PersianCalendar();
      DateTime date1 = new DateTime(1389, 5, 27, 16, 32, 18, 500, persian);
      Console.WriteLine(date1.ToString("M/dd/yyyy h:mm:ss.fff tt"));
      Console.WriteLine("{0}/{1}/{2} {3}{7}{4:D2}{7}{5:D2}.{6:G3}\n",
                                       persian.GetMonth(date1),
                                       persian.GetDayOfMonth(date1),
                                       persian.GetYear(date1),
                                       persian.GetHour(date1),
                                       persian.GetMinute(date1),
                                       persian.GetSecond(date1),
                                       persian.GetMilliseconds(date1),
                                       DateTimeFormatInfo.CurrentInfo.TimeSeparator);

      Console.WriteLine("Using the Hijri Calendar:");
      // Get current culture so it can later be restored.
      CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

      // Define strings for use in composite formatting.
      string dFormat;
      string fmtString;
      // Define Hijri calendar.
      HijriCalendar hijri = new HijriCalendar();
      // Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
      CultureInfo current = CultureInfo.CurrentCulture;
      current.DateTimeFormat.Calendar = hijri;
      dFormat = current.DateTimeFormat.ShortDatePattern;
      // Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy") + " H:mm:ss.fff";
      fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "}";
      DateTime date2 = new DateTime(1431, 9, 9, 16, 32, 18, 500, hijri);
      Console.WriteLine(fmtString, current, GetCalendarName(hijri), date2);

      // Restore previous culture.
      Thread.CurrentThread.CurrentCulture = dftCulture;
      dFormat = DateTimeFormatInfo.CurrentInfo.ShortDatePattern +" H:mm:ss.fff";
      fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "}";
      Console.WriteLine(fmtString,
                        CultureInfo.CurrentCulture,
                        GetCalendarName(CultureInfo.CurrentCulture.Calendar),
                        date2);
   }

   private static string GetCalendarName(Calendar cal)
   {
      return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
   }
}
// The example displays the following output:
//       8/18/2010 4:32:18.500 PM
//       5/27/1389 16:32:18.500
//
//       Using the Hijri Calendar:
//       ar-SY culture using the Hijri calendar: 09/09/1431 16:32:18.500
//       en-US culture using the Gregorian calendar: 8/18/2010 16:32:18.500

Remarks

The Kind property is initialized to Unspecified.

The allowable values for year, month, and day depend on calendar. An exception is thrown if the specified date and time cannot be expressed using calendar.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

Important

Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. For example, May 1, 2019 marked the beginning of the Reiwa era in the JapaneseCalendar and JapaneseLunisolarCalendar. Such a change of era affects all applications that use these calendars. For more information and to determine whether your applications are affected, see Handling a new era in the Japanese calendar in .NET. For information on testing your applications on Windows systems to ensure their readiness for the era change, see Prepare your application for the Japanese era change. For features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras, see Working with eras.

The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET 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 2.0, 2.1

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time.

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

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

kind
DateTimeKind

One of the enumeration values that indicates whether year, month, day, hour, minute, second, and millisecond specify a local time, Coordinated Universal Time (UTC), or neither.

Exceptions

year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

kind is not one of the DateTimeKind values.

Examples

The following example uses the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) constructor to instantiate a DateTime value.

C#
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 18, 500,
                              DateTimeKind.Local);
Console.WriteLine("{0:M/dd/yyyy h:mm:ss.fff tt} {1}", date1, date1.Kind);
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:18.500 PM Local

Remarks

This constructor interpretsyear, month, and day as a year, month, and day in the Gregorian calendar. To instantiate a DateTime value by using the year, month, and day in another calendar, call the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) constructor.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

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 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.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond.

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

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

Exceptions

year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

Examples

The following example uses the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32) constructor to instantiate a DateTime value.

C#
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 18, 500);
Console.WriteLine(date1.ToString("M/dd/yyyy h:mm:ss.fff tt"));
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:18.500 PM

Remarks

This constructor interpretsyear, month, and day as a year, month, and day in the Gregorian calendar. To instantiate a DateTime value by using the year, month, and day in another calendar, call the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) constructor.

The Kind property is initialized to Unspecified.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

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.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, millisecond, and Coordinated Universal Time (UTC) or local time for the specified calendar.

C#
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar, DateTimeKind kind);

Parameters

year
Int32

The year (1 through the number of years in calendar).

month
Int32

The month (1 through the number of months in calendar).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

millisecond
Int32

The milliseconds (0 through 999).

microsecond
Int32

The microseconds (0 through 999).

calendar
Calendar

The calendar that is used to interpret year, month, and day.

kind
DateTimeKind

One of the enumeration values that indicates whether year, month, day, hour, minute, second, and millisecond specify a local time, Coordinated Universal Time (UTC), or neither.

Exceptions

calendar is null

year is not in the range supported by calendar.

-or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

-or-

millisecond is less than 0 or greater than 999.

-or-

microsecond is less than 0 or greater than 999.

kind is not one of the DateTimeKind values.

Remarks

The allowable values for year, month, and day parameters depend on the calendar parameter. An exception is thrown if the specified date and time cannot be expressed using calendar.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and Coordinated Universal Time (UTC) or local time.

C#
public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

kind
DateTimeKind

One of the enumeration values that indicates whether year, month, day, hour, minute and second specify a local time, Coordinated Universal Time (UTC), or neither.

Exceptions

year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

kind is not one of the DateTimeKind values.

Examples

The following example uses the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) constructor to instantiate a DateTime value.

C#
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 0, DateTimeKind.Local);
Console.WriteLine("{0} {1}", date1, date1.Kind);
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:00 PM Local

Remarks

This constructor interpretsyear, month, and day as a year, month, and day in the Gregorian calendar. To instantiate a DateTime value by using the year, month, and day in another calendar, call the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) constructor.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

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 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.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Int32, Int32, Int32)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

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

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

Exceptions

year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23.

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

Examples

The following example uses the DateTime constructor to instantiate a DateTime value.

C#
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 0);
Console.WriteLine(date1.ToString());
// The example displays the following output, in this case for en-us culture:
//      8/18/2010 4:32:00 PM

Remarks

The Kind property is initialized to Unspecified.

This constructor interpretsyear, month, and day as a year, month, and day in the Gregorian calendar. To instantiate a DateTime value by using the year, month, and day in another calendar, call the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) constructor.

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

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.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(Int32, Int32, Int32, Calendar)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, and day for the specified calendar.

C#
public DateTime(int year, int month, int day, System.Globalization.Calendar calendar);

Parameters

year
Int32

The year (1 through the number of years in calendar).

month
Int32

The month (1 through the number of months in calendar).

day
Int32

The day (1 through the number of days in month).

calendar
Calendar

The calendar that is used to interpret year, month, and day.

Exceptions

calendar is null.

year is not in the range supported by calendar.

-or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

Examples

The following example calls the DateTime(Int32, Int32, Int32, Calendar) constructor twice to instantiate two DateTime values. The first call instantiates a DateTime value by using a PersianCalendar object. Because the Persian calendar cannot be designated as the default calendar for a culture, displaying a date in the Persian calendar requires individual calls to its PersianCalendar.GetMonth, PersianCalendar.GetDayOfMonth, and PersianCalendar.GetYear methods. The second call to the constructor instantiates a DateTime value by using a HijriCalendar object. The example changes the current culture to Arabic (Syria) and changes the current culture's default calendar to the Hijri calendar. Because Hijri is the current culture's default calendar, the Console.WriteLine method uses it to format the date. When the previous current culture (which is English (United States) in this case) is restored, the Console.WriteLine method uses the current culture's default Gregorian calendar to format the date.

C#
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Using the Persian Calendar:");
      PersianCalendar persian = new PersianCalendar();
      DateTime date1 = new DateTime(1389, 5, 27, persian);
      Console.WriteLine(date1.ToString());
      Console.WriteLine("{0}/{1}/{2}\n", persian.GetMonth(date1),
                                       persian.GetDayOfMonth(date1),
                                       persian.GetYear(date1));

      Console.WriteLine("Using the Hijri Calendar:");
      // Get current culture so it can later be restored.
      CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

      // Define Hijri calendar.
      HijriCalendar hijri = new HijriCalendar();
      // Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
      CultureInfo current = CultureInfo.CurrentCulture;
      current.DateTimeFormat.Calendar = hijri;
      string dFormat = current.DateTimeFormat.ShortDatePattern;
      // Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy");
      current.DateTimeFormat.ShortDatePattern = dFormat;
      DateTime date2 = new DateTime(1431, 9, 9, hijri);
      Console.WriteLine("{0} culture using the {1} calendar: {2:d}", current,
                        GetCalendarName(hijri), date2);

      // Restore previous culture.
      Thread.CurrentThread.CurrentCulture = dftCulture;
      Console.WriteLine("{0} culture using the {1} calendar: {2:d}",
                        CultureInfo.CurrentCulture,
                        GetCalendarName(CultureInfo.CurrentCulture.Calendar),
                        date2);
   }

   private static string GetCalendarName(Calendar cal)
   {
      return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
   }
}
// The example displays the following output:
//       Using the Persian Calendar:
//       8/18/2010 12:00:00 AM
//       5/27/1389
//
//       Using the Hijri Calendar:
//       ar-SY culture using the Hijri calendar: 09/09/1431
//       en-US culture using the Gregorian calendar: 8/18/2010

Remarks

The time of day for the resulting DateTime is midnight (00:00:00). The Kind property is initialized to Unspecified.

The allowable values for year, month, and day depend on calendar. An exception is thrown if the specified date and time cannot be expressed using calendar.

Important

Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. For example, May 1, 2019 marked the beginning of the Reiwa era in the JapaneseCalendar and JapaneseLunisolarCalendar. Such a change of era affects all applications that use these calendars. For more information and to determine whether your applications are affected, see Handling a new era in the Japanese calendar in .NET. For information on testing your applications on Windows systems to ensure their readiness for the era change, see Prepare your application for the Japanese era change. For features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras, see Working with eras.

The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET 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 2.0, 2.1

DateTime(Int32, Int32, Int32)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, and day.

C#
public DateTime(int year, int month, int day);

Parameters

year
Int32

The year (1 through 9999).

month
Int32

The month (1 through 12).

day
Int32

The day (1 through the number of days in month).

Exceptions

year is less than 1 or greater than 9999.

-or-

month is less than 1 or greater than 12.

-or-

day is less than 1 or greater than the number of days in month.

Examples

The following example uses the DateTime(Int32, Int32, Int32) constructor to instantiate a DateTime value. The example also illustrates that this overload creates a DateTime value whose time component equals midnight (or 0:00).

C#
DateTime date1 = new DateTime(2010, 8, 18);
Console.WriteLine(date1.ToString());
// The example displays the following output:
//      8/18/2010 12:00:00 AM

Remarks

This constructor interprets year, month, and day as a year, month, and day in the Gregorian calendar. To instantiate a DateTime value by using the year, month, and day in another calendar, call the DateTime(Int32, Int32, Int32, Calendar) constructor.

The time of day for the resulting DateTime is midnight (00:00:00). The Kind property is initialized to DateTimeKind.Unspecified.

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.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(DateOnly, TimeOnly, DateTimeKind)

Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified DateOnly and TimeOnly and respecting the specified DateTimeKind.

C#
public DateTime(DateOnly date, TimeOnly time, DateTimeKind kind);

Parameters

date
DateOnly

The date part.

time
TimeOnly

The time part.

kind
DateTimeKind

One of the enumeration values that indicates whether date and time specify a local time, Coordinated Universal Time (UTC), or neither.

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

DateTime(Int64, DateTimeKind)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to a specified number of ticks and to Coordinated Universal Time (UTC) or local time.

C#
public DateTime(long ticks, DateTimeKind kind);

Parameters

ticks
Int64

A date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.

kind
DateTimeKind

One of the enumeration values that indicates whether ticks specifies a local time, Coordinated Universal Time (UTC), or neither.

Exceptions

kind is not one of the DateTimeKind values.

Remarks

For applications in which portability of date and time data or a limited degree of time zone awareness is important, you can use the corresponding DateTimeOffset constructor.

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 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.5, 1.6, 2.0, 2.1
UWP 10.0

DateTime(DateOnly, TimeOnly)

Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified DateOnly and TimeOnly. The new instance will have the Unspecified kind.

C#
public DateTime(DateOnly date, TimeOnly time);

Parameters

date
DateOnly

The date part.

time
TimeOnly

The time part.

Applies to

.NET 9 and .NET 8
Product Versions
.NET 8, 9

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

Source:
DateTime.cs
Source:
DateTime.cs
Source:
DateTime.cs

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second for the specified calendar.

C#
public DateTime(int year, int month, int day, int hour, int minute, int second, System.Globalization.Calendar calendar);

Parameters

year
Int32

The year (1 through the number of years in calendar).

month
Int32

The month (1 through the number of months in calendar).

day
Int32

The day (1 through the number of days in month).

hour
Int32

The hours (0 through 23).

minute
Int32

The minutes (0 through 59).

second
Int32

The seconds (0 through 59).

calendar
Calendar

The calendar that is used to interpret year, month, and day.

Exceptions

calendar is null.

year is not in the range supported by calendar.

-or-

month is less than 1 or greater than the number of months in calendar.

-or-

day is less than 1 or greater than the number of days in month.

-or-

hour is less than 0 or greater than 23

-or-

minute is less than 0 or greater than 59.

-or-

second is less than 0 or greater than 59.

Examples

The following example calls the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) constructor twice to instantiate two DateTime values. The first call instantiates a DateTime value by using a PersianCalendar object. Because the Persian calendar cannot be designated as the default calendar for a culture, displaying a date in the Persian calendar requires individual calls to its PersianCalendar.GetMonth, PersianCalendar.GetDayOfMonth, and PersianCalendar.GetYear methods. The second call to the constructor instantiates a DateTime value by using a HijriCalendar object. The example changes the current culture to Arabic (Syria) and changes the current culture's default calendar to the Hijri calendar. Because Hijri is the current culture's default calendar, the Console.WriteLine method uses it to format the date. When the previous current culture (which is English (United States) in this case) is restored, the Console.WriteLine method uses the current culture's default Gregorian calendar to format the date.

C#
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Using the Persian Calendar:");
      PersianCalendar persian = new PersianCalendar();
      DateTime date1 = new DateTime(1389, 5, 27, 16, 32, 0, persian);
      Console.WriteLine(date1.ToString());
      Console.WriteLine("{0}/{1}/{2} {3}{6}{4:D2}{6}{5:D2}\n",
                                       persian.GetMonth(date1),
                                       persian.GetDayOfMonth(date1),
                                       persian.GetYear(date1),
                                       persian.GetHour(date1),
                                       persian.GetMinute(date1),
                                       persian.GetSecond(date1),
                                       DateTimeFormatInfo.CurrentInfo.TimeSeparator);

      Console.WriteLine("Using the Hijri Calendar:");
      // Get current culture so it can later be restored.
      CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

      // Define Hijri calendar.
      HijriCalendar hijri = new HijriCalendar();
      // Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
      CultureInfo current = CultureInfo.CurrentCulture;
      current.DateTimeFormat.Calendar = hijri;
      string dFormat = current.DateTimeFormat.ShortDatePattern;
      // Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy");
      current.DateTimeFormat.ShortDatePattern = dFormat;
      DateTime date2 = new DateTime(1431, 9, 9, 16, 32, 18, hijri);
      Console.WriteLine("{0} culture using the {1} calendar: {2:g}", current,
                        GetCalendarName(hijri), date2);

      // Restore previous culture.
      Thread.CurrentThread.CurrentCulture = dftCulture;
      Console.WriteLine("{0} culture using the {1} calendar: {2:g}",
                        CultureInfo.CurrentCulture,
                        GetCalendarName(CultureInfo.CurrentCulture.Calendar),
                        date2);
   }

   private static string GetCalendarName(Calendar cal)
   {
      return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
   }
}
// The example displays the following output:
//       Using the Persian Calendar:
//       8/18/2010 4:32:00 PM
//       5/27/1389 16:32:00
//
//       Using the Hijri Calendar:
//       ar-SY culture using the Hijri calendar: 09/09/1431 04:32 م
//       en-US culture using the Gregorian calendar: 8/18/2010 4:32 PM

Remarks

The Kind property is initialized to Unspecified.

The allowable values for year, month, and day depend on calendar. An exception is thrown if the specified date and time cannot be expressed using calendar.

Important

Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. For example, May 1, 2019 marked the beginning of the Reiwa era in the JapaneseCalendar and JapaneseLunisolarCalendar. Such a change of era affects all applications that use these calendars. For more information and to determine whether your applications are affected, see Handling a new era in the Japanese calendar in .NET. For information on testing your applications on Windows systems to ensure their readiness for the era change, see Prepare your application for the Japanese era change. For features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras, see Working with eras.

The System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET 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 2.0, 2.1