DateTime 构造函数

定义

初始化 DateTime 结构的新实例。

重载

DateTime(Int64)

DateTime 结构的新实例初始化为指定的刻度数。

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒和毫秒。

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

DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟、秒和毫秒。

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒和协调世界时 (UTC) 或本地时间。

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

DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟和秒。

DateTime(Int32, Int32, Int32, Calendar)

DateTime 结构的新实例初始化为指定日历的指定年、月和日。

DateTime(Int32, Int32, Int32)

DateTime 结构的新实例初始化为指定的年、月和日。

DateTime(DateOnly, TimeOnly, DateTimeKind)

将 结构的新实例 DateTime 初始化为指定的 DateOnlyTimeOnly ,并遵循指定的 DateTimeKind

DateTime(Int64, DateTimeKind)

DateTime 结构的新实例初始化为指定的计时周期数以及协调世界时 (UTC) 或本地时间。

DateTime(DateOnly, TimeOnly)

将 结构的新实例 DateTime 初始化为指定的 DateOnlyTimeOnly。 新实例将具有 类型 Unspecified

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

DateTime 结构的新实例初始化为指定日历的年、月、日、小时、分钟和秒。

DateTime(Int64)

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

DateTime 结构的新实例初始化为指定的刻度数。

C#
public DateTime (long ticks);

参数

ticks
Int64

一个日期和时间,以公历 0001 年 1 月 1 日 00:00:00.000 以来所经历的以 100 纳秒为间隔的间隔数来表示。

例外

示例

以下示例演示了其中 DateTime 一个构造函数。

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.

*/

注解

Kind 属性初始化为 Unspecified

对于日期和时间数据的可移植性或有限时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

参数

year
Int32

年(1 到 calendar 中的年数)。

month
Int32

月(1 到 calendar 中的月数)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

millisecond
Int32

毫秒(0 到 999)。

microsecond
Int32

微秒 (0 到 999) 。

calendar
Calendar

用于解释 yearmonthday 的日历。

例外

calendarnull

year 不在 calendar 支持的范围内。

month 小于 1 或大于 calendar 中的月数。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

millisecond 小于 0 或大于 999。

- 或 -

microsecond 小于 0 或大于 999。

注解

monthday 参数的year允许值取决于 calendar 参数。 如果无法使用 表示 calendar指定的日期和时间,则会引发异常。

对于日期和时间数据的可移植性或有限时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.NET 7, 8, 9

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

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

参数

year
Int32

年(1 到 9999)。

month
Int32

月(1 到 12)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

millisecond
Int32

毫秒(0 到 999)。

microsecond
Int32

微秒 (0 到 999) 。

kind
DateTimeKind

枚举值之一,该值指示 yearmonthday, hourminutesecondmillisecond 指定了本地时间、协调世界时 (UTC),还是两者皆未指定。

例外

year 小于 1 或大于 9999。

month 小于 1 或大于 12。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

millisecond 小于 0 或大于 999。

- 或 -

microsecond 小于 0 或大于 999。

kind 不是 DateTimeKind 值之一。

注解

此构造函数将 yearmonthday 解释为公历中的年、月和日。 若要在另一个 DateTime 日历中使用年、月和日来实例化值,请 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 调用 构造函数。

对于日期和时间数据的可移植性或有限程度的时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.NET 7, 8, 9

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

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

参数

year
Int32

年(1 到 calendar 中的年数)。

month
Int32

月(1 到 calendar 中的月数)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

millisecond
Int32

毫秒(0 到 999)。

calendar
Calendar

用于解释 yearmonthday 的日历。

kind
DateTimeKind

枚举值之一,该值指示 yearmonthday, hourminutesecondmillisecond 指定了本地时间、协调世界时 (UTC),还是两者皆未指定。

例外

calendar 上声明的默认值为 null

year 不在 calendar 支持的范围内。

month 小于 1 或大于 calendar 中的月数。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

millisecond 小于 0 或大于 999。

kind 不是 DateTimeKind 值之一。

示例

以下示例调用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 构造函数两次以实例化两个 DateTime 值。 第一个 DateTime 调用通过使用 PersianCalendar 对象实例化值。 由于无法将波斯历指定为区域性的默认日历,因此在波斯日历中显示日期需要单独调用其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonthPersianCalendar.GetYear 方法。 对构造函数的第二次DateTimeHijriCalendar调用通过使用 对象实例化值。 该示例将当前区域性更改为阿拉伯语 (叙利亚) ,并将当前区域性的默认日历更改为回历。 由于 Hijri 是当前区域性的默认日历,因此该方法 Console.WriteLine 使用它来设置日期格式。 当上一个当前区域性 ((在本例中为英语 (美国) ) )还原时,Console.WriteLine该方法使用当前区域性的默认公历来设置日期格式。

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

注解

monthday 参数的year允许值取决于 calendar 参数。 如果无法使用 来表示 calendar指定的日期和时间,则会引发异常。

对于日期和时间数据的可移植性或有限程度的时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其准备好进行纪元更改的信息,请参阅 准备应用程序应对日本纪元更改。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元

命名空间 System.Globalization 提供多个日历,包括 GregorianCalendarJulianCalendar

适用于

.NET 9 和其他版本
产品 版本
.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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

参数

year
Int32

年(1 到 9999)。

month
Int32

月(1 到 12)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

millisecond
Int32

毫秒(0 到 999)。

microsecond
Int32

微秒 (0 到 999) 。

例外

year 小于 1 或大于 9999。

month 小于 1 或大于 12。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

millisecond 小于 0 或大于 999。

- 或 -

microsecond 小于 0 或大于 999。

注解

此构造函数将 yearmonthday 解释为公历中的年、月和日。 若要在另一个 DateTime 日历中使用年、月和日来实例化值,请 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 调用 构造函数。

Kind 属性初始化为 Unspecified

对于日期和时间数据的可移植性或有限程度的时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.NET 7, 8, 9

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

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒和毫秒。

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

参数

year
Int32

年(1 到 calendar 中的年数)。

month
Int32

月(1 到 calendar 中的月数)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

millisecond
Int32

毫秒(0 到 999)。

calendar
Calendar

用于解释 yearmonthday 的日历。

例外

calendar 上声明的默认值为 null

year 不在 calendar 支持的范围内。

month 小于 1 或大于 calendar 中的月数。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

millisecond 小于 0 或大于 999。

示例

以下示例调用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 构造函数两次以实例化两个 DateTime 值。 第一个 DateTime 调用通过使用 PersianCalendar 对象实例化值。 由于无法将波斯历指定为区域性的默认日历,因此在波斯日历中显示日期需要单独调用其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonthPersianCalendar.GetYear 方法。 对构造函数的第二次DateTimeHijriCalendar调用通过使用 对象实例化值。 该示例将当前区域性更改为阿拉伯语 (叙利亚) ,并将当前区域性的默认日历更改为回历。 由于 Hijri 是当前区域性的默认日历,因此该方法 Console.WriteLine 使用它来设置日期格式。 当上一个当前区域性 ((在本例中为英语 (美国) ) )还原时,Console.WriteLine该方法使用当前区域性的默认公历来设置日期格式。

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

注解

Kind 属性初始化为 Unspecified

、 和 的year允许值取决于 calendardaymonth 如果无法使用 来表示 calendar指定的日期和时间,则会引发异常。

对于日期和时间数据的可移植性或有限程度的时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其准备好进行纪元更改的信息,请参阅 准备应用程序应对日本纪元更改。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元

命名空间 System.Globalization 提供多个日历,包括 GregorianCalendarJulianCalendar

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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

参数

year
Int32

年(1 到 9999)。

month
Int32

月(1 到 12)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

millisecond
Int32

毫秒(0 到 999)。

kind
DateTimeKind

枚举值之一,该值指示 yearmonthday, hourminutesecondmillisecond 指定了本地时间、协调世界时 (UTC),还是两者皆未指定。

例外

year 小于 1 或大于 9999。

month 小于 1 或大于 12。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

millisecond 小于 0 或大于 999。

kind 不是 DateTimeKind 值之一。

示例

下面的示例使用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) 构造函数来实例化 DateTime 值。

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

注解

此构造函数将yearmonth 、 和 day 解释为公历中的年、月和日。 若要在另一个 DateTime 日历中使用年、月和日来实例化值,请 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 调用 构造函数。

对于日期和时间数据的可移植性或有限程度的时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.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

DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟、秒和毫秒。

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

参数

year
Int32

年(1 到 9999)。

month
Int32

月(1 到 12)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

millisecond
Int32

毫秒(0 到 999)。

例外

year 小于 1 或大于 9999。

month 小于 1 或大于 12。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

millisecond 小于 0 或大于 999。

示例

下面的示例使用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32) 构造函数来实例化 DateTime 值。

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

注解

此构造函数将yearmonth 、 和 day 解释为公历中的年、月和日。 若要在另一个 DateTime 日历中使用年、月和日来实例化值,请 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 调用 构造函数。

Kind 属性初始化为 Unspecified

对于日期和时间数据的可移植性或有限程度的时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。

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);

参数

year
Int32

年(1 到 calendar 中的年数)。

month
Int32

月(1 到 calendar 中的月数)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

millisecond
Int32

毫秒(0 到 999)。

microsecond
Int32

微秒 (0 到 999) 。

calendar
Calendar

用于解释 yearmonthday 的日历。

kind
DateTimeKind

枚举值之一,该值指示 yearmonthday, hourminutesecondmillisecond 指定了本地时间、协调世界时 (UTC),还是两者皆未指定。

例外

calendarnull

year 不在 calendar 支持的范围内。

month 小于 1 或大于 calendar 中的月数。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

millisecond 小于 0 或大于 999。

- 或 -

microsecond 小于 0 或大于 999。

kind 不是 DateTimeKind 值之一。

注解

monthday 参数的year允许值取决于 calendar 参数。 如果无法使用 来表示 calendar指定的日期和时间,则会引发异常。

对于日期和时间数据的可移植性或有限程度的时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.NET 7, 8, 9

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

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

DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒和协调世界时 (UTC) 或本地时间。

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

参数

year
Int32

年(1 到 9999)。

month
Int32

月(1 到 12)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

kind
DateTimeKind

枚举值之一,该值指示 yearmonthdayhourminutesecond 指定了本地时间、协调世界时 (UTC),还是两者皆未指定。

例外

year 小于 1 或大于 9999。

month 小于 1 或大于 12。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

kind 不是 DateTimeKind 值之一。

示例

以下示例使用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) 构造函数来实例化 DateTime 值。

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

注解

此构造函数将yearmonth 、 和 day 解释为公历中的年、月和日。 若要在另一个 DateTime 日历中使用年、月和日实例化值,请调用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 构造函数。

对于日期和时间数据的可移植性或有限时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.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

DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟和秒。

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

参数

year
Int32

年(1 到 9999)。

month
Int32

月(1 到 12)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

例外

year 小于 1 或大于 9999。

month 小于 1 或大于 12。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23。

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

示例

以下示例使用 DateTime 构造函数来实例化 DateTime 值。

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

注解

Kind 属性初始化为 Unspecified

此构造函数将yearmonth 、 和 day 解释为公历中的年、月和日。 若要在另一个 DateTime 日历中使用年、月和日实例化值,请调用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 构造函数。

对于日期和时间数据的可移植性或有限时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.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

DateTime 结构的新实例初始化为指定日历的指定年、月和日。

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

参数

year
Int32

年(1 到 calendar 中的年数)。

month
Int32

月(1 到 calendar 中的月数)。

day
Int32

日(1 到 month 中的天数)。

calendar
Calendar

用于解释 yearmonthday 的日历。

例外

calendar 上声明的默认值为 null

year 不在 calendar 支持的范围内。

month 小于 1 或大于 calendar 中的月数。

day 小于 1 或大于 month中的天数。

示例

以下示例调用 DateTime(Int32, Int32, Int32, Calendar) 构造函数两次以实例化两个 DateTime 值。 第一个 DateTime 调用通过使用 对象实例化值 PersianCalendar 。 由于无法将波斯历指定为区域性的默认日历,因此在波斯日历中显示日期需要单独调用其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonthPersianCalendar.GetYear 方法。 对构造函数的第二次HijriCalendar调用通过使用 对象实例化DateTime值。 该示例将当前区域性更改为阿拉伯语 (叙利亚) ,并将当前区域性的默认日历更改为 Hijri 日历。 由于 Hijri 是当前区域性的默认日历,因此 该方法 Console.WriteLine 使用它来设置日期的格式。 当还原上一个当前区域性 ((在本例中为英语 (美国) ) )时,Console.WriteLine方法使用当前区域性的默认公历来设置日期格式。

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

注解

生成的 DateTime 一天中的时间为午夜 (00:00:00) 。 将 Kind 属性初始化为 Unspecified

、 和 的year允许值取决于 calendardaymonth 如果无法使用 表示 calendar指定的日期和时间,则会引发异常。

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其为时代更改做好准备的信息,请参阅 为日本时代更改准备应用程序。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元

命名空间 System.Globalization 提供多个日历,包括 GregorianCalendarJulianCalendar

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

DateTime 结构的新实例初始化为指定的年、月和日。

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

参数

year
Int32

年(1 到 9999)。

month
Int32

月(1 到 12)。

day
Int32

日(1 到 month 中的天数)。

例外

year 小于 1 或大于 9999。

month 小于 1 或大于 12。

day 小于 1 或大于 month中的天数。

示例

以下示例使用 DateTime(Int32, Int32, Int32) 构造函数来实例化 DateTime 值。 该示例还演示了此重载创建一个 DateTime 值,其时间组件等于午夜 (或 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

注解

此构造函数将 yearmonthday 解释为公历中的年、月和日。 若要在另一个 DateTime 日历中使用年、月和日实例化值,请调用 DateTime(Int32, Int32, Int32, Calendar) 构造函数。

生成的 DateTime 一天中的时间为午夜 (00:00:00) 。 将 Kind 属性初始化为 DateTimeKind.Unspecified

适用于

.NET 9 和其他版本
产品 版本
.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

将 结构的新实例 DateTime 初始化为指定的 DateOnlyTimeOnly ,并遵循指定的 DateTimeKind

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

参数

date
DateOnly

日期部分。

time
TimeOnly

时间部分。

kind
DateTimeKind

枚举值之一,指示 datetime 指定本地时间、协调世界时 (UTC) ,或者两者都不指定。

适用于

.NET 9 和 .NET 8
产品 版本
.NET 8, 9

DateTime(Int64, DateTimeKind)

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

DateTime 结构的新实例初始化为指定的计时周期数以及协调世界时 (UTC) 或本地时间。

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

参数

ticks
Int64

一个日期和时间,以公历 0001 年 1 月 1 日 00:00:00.000 以来所经历的以 100 纳秒为间隔的间隔数来表示。

kind
DateTimeKind

枚举值之一,该值指示 ticks 是指定了本地时间、协调世界时 (UTC),还是两者皆未指定。

例外

kind 不是 DateTimeKind 值之一。

注解

对于日期和时间数据的可移植性或有限程度的时区感知非常重要的应用程序,可以使用相应的 DateTimeOffset 构造函数。

适用于

.NET 9 和其他版本
产品 版本
.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

将 结构的新实例 DateTime 初始化为指定的 DateOnlyTimeOnly。 新实例将具有 类型 Unspecified

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

参数

date
DateOnly

日期部分。

time
TimeOnly

时间部分。

适用于

.NET 9 和 .NET 8
产品 版本
.NET 8, 9

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

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

DateTime 结构的新实例初始化为指定日历的年、月、日、小时、分钟和秒。

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

参数

year
Int32

年(1 到 calendar 中的年数)。

month
Int32

月(1 到 calendar 中的月数)。

day
Int32

日(1 到 month 中的天数)。

hour
Int32

小时(0 到 23)。

minute
Int32

分(0 到 59)。

second
Int32

秒(0 到 59)。

calendar
Calendar

用于解释 yearmonthday 的日历。

例外

calendar 上声明的默认值为 null

year 不在 calendar 支持的范围内。

month 小于 1 或大于 calendar 中的月数。

day 小于 1 或大于 month中的天数。

hour 小于 0 或大于 23

minute 小于 0 或大于 59。

second 小于 0 或大于 59。

示例

以下示例调用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 构造函数两次以实例化两个 DateTime 值。 第一个 DateTime 调用通过使用 PersianCalendar 对象实例化值。 由于无法将波斯历指定为区域性的默认日历,因此在波斯日历中显示日期需要单独调用其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonthPersianCalendar.GetYear 方法。 对构造函数的第二次DateTimeHijriCalendar调用通过使用 对象实例化值。 该示例将当前区域性更改为阿拉伯语 (叙利亚) ,并将当前区域性的默认日历更改为回历。 由于 Hijri 是当前区域性的默认日历,因此该方法 Console.WriteLine 使用它来设置日期格式。 当上一个当前区域性 ((在本例中为英语 (美国) ) )还原时,Console.WriteLine该方法使用当前区域性的默认公历来设置日期格式。

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

注解

Kind 属性初始化为 Unspecified

、 和 的year允许值取决于 calendardaymonth 如果无法使用 来表示 calendar指定的日期和时间,则会引发异常。

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其准备好进行纪元更改的信息,请参阅 准备应用程序应对日本纪元更改。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元

命名空间 System.Globalization 提供多个日历,包括 GregorianCalendarJulianCalendar

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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