DateTime DateTime DateTime DateTime Constructors

定义

重载

DateTime(Int64) DateTime(Int64) DateTime(Int64) DateTime(Int64)

DateTime 结构的新实例初始化为指定的刻度数。Initializes a new instance of the DateTime structure to a specified number of ticks.

DateTime(Int64, DateTimeKind) DateTime(Int64, DateTimeKind) DateTime(Int64, DateTimeKind) DateTime(Int64, DateTimeKind)

DateTime 结构的新实例初始化为指定的计时周期数以及协调世界时 (UTC) 或本地时间。Initializes a new instance of the DateTime structure to a specified number of ticks and to Coordinated Universal Time (UTC) or local time.

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

DateTime 结构的新实例初始化为指定的年、月和日。Initializes a new instance of the DateTime structure to the specified year, month, and day.

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

DateTime 结构的新实例初始化为指定日历的指定年、月和日。Initializes a new instance of the DateTime structure to the specified year, month, and day for the specified calendar.

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

DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟和秒。Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

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

DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒和协调世界时 (UTC) 或本地时间。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, Calendar) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

DateTime 结构的新实例初始化为指定日历的年、月、日、小时、分钟和秒。Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second for the specified calendar.

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

DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟、秒和毫秒。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, DateTimeKind) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (UTC) 或本地时间。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, Calendar) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒和毫秒。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, Calendar, DateTimeKind) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

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

DateTime 结构的新实例初始化为指定的刻度数。Initializes a new instance of the DateTime structure to a specified number of ticks.

public:
 DateTime(long ticks);
public DateTime (long ticks);
new DateTime : int64 -> DateTime
Public Sub New (ticks As Long)

参数

ticks
Int64 Int64 Int64 Int64

一个日期和时间,以公历 0001 年 1 月 1 日 00:00:00.000 以来所经历的以 100 纳秒为间隔的间隔数来表示。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.

异常

示例

下面的示例演示 @no__t 0 构造函数之一。The following example demonstrates one of the DateTime constructors.

// This example demonstrates the DateTime(Int64) constructor.
using namespace System;
using namespace System::Globalization;
int 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 = DateTime(DateTime::MaxValue.Ticks);
   
   // Create a DateTime for the minimum date and time using ticks.
   DateTime dt2 = 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. 
   Int64 ticks = DateTime(1979,07,28,22,35,5,(gcnew CultureInfo( "en-US",false ))->Calendar).Ticks;
   DateTime dt3 = 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.

*/
// 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.

*/
' This example demonstrates the DateTime(Int64) constructor.
Imports System.Globalization

Class Sample
   Public Shared Sub 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.
      Dim format As String = "{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.
      Dim dt1 As New DateTime(DateTime.MaxValue.Ticks)
      
      ' Create a DateTime for the minimum date and time using ticks.
      Dim dt2 As 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. 
      Dim ticks As Long = New DateTime(1979, 7, 28, 22, 35, 5, _
                                       New CultureInfo("en-US", False).Calendar).Ticks
      Dim dt3 As New DateTime(ticks)
      
      Console.WriteLine(format, 1, "maximum", dt1)
      Console.WriteLine(format, 2, "minimum", dt2)
      Console.WriteLine(format, 3, "custom ", dt3)
      Console.WriteLine(vbCrLf & "The custom date and time is created from {0:N0} ticks.", ticks)
   End Sub
End Class
'
'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 属性初始化为 UnspecifiedThe Kind property is initialized to Unspecified.

如果应用程序的日期和时间数据的可移植性或有限程度的时区感知非常重要,则可以使用相应的 DateTimeOffset 构造函数。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.

DateTime(Int64, DateTimeKind) DateTime(Int64, DateTimeKind) DateTime(Int64, DateTimeKind) DateTime(Int64, DateTimeKind)

DateTime 结构的新实例初始化为指定的计时周期数以及协调世界时 (UTC) 或本地时间。Initializes a new instance of the DateTime structure to a specified number of ticks and to Coordinated Universal Time (UTC) or local time.

public:
 DateTime(long ticks, DateTimeKind kind);
public DateTime (long ticks, DateTimeKind kind);
new DateTime : int64 * DateTimeKind -> DateTime
Public Sub New (ticks As Long, kind As DateTimeKind)

参数

ticks
Int64 Int64 Int64 Int64

一个日期和时间,以公历 0001 年 1 月 1 日 00:00:00.000 以来所经历的以 100 纳秒为间隔的间隔数来表示。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 DateTimeKind DateTimeKind DateTimeKind

枚举值之一,该值指示 ticks 是指定了本地时间、协调世界时 (UTC),还是两者皆未指定。One of the enumeration values that indicates whether ticks specifies a local time, Coordinated Universal Time (UTC), or neither.

异常

kind 不是 DateTimeKind 值之一。kind is not one of the DateTimeKind values.

注解

如果应用程序的日期和时间数据的可移植性或有限程度的时区感知非常重要,则可以使用相应的 DateTimeOffset 构造函数。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.

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

DateTime 结构的新实例初始化为指定的年、月和日。Initializes a new instance of the DateTime structure to the specified year, month, and day.

public:
 DateTime(int year, int month, int day);
public DateTime (int year, int month, int day);
new DateTime : int * int * int -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer)

参数

year
Int32 Int32 Int32 Int32

年(1 到 9999)。The year (1 through 9999).

month
Int32 Int32 Int32 Int32

月(1 到 12)。The month (1 through 12).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

异常

year 小于 1 或大于 9999。year is less than 1 or greater than 9999.

-or- month 小于 1 或大于 12。month is less than 1 or greater than 12.

- 或 --or- day 小于 1 或大于 month 中的天数。day is less than 1 or greater than the number of days in month.

示例

下面的示例使用 DateTime(Int32, Int32, Int32) 构造函数来实例化 @no__t 1 值。The following example uses the DateTime(Int32, Int32, Int32) constructor to instantiate a DateTime value. 该示例还演示了此重载创建一个 @no__t 0 值,其时间组成部分等于午夜(或0:00)。The example also illustrates that this overload creates a DateTime value whose time component equals midnight (or 0:00).

DateTime date1 = new DateTime(2010, 8, 18);
Console.WriteLine(date1.ToString());
// The example displays the following output:
//      8/18/2010 12:00:00 AM      
Dim date1 As New Date(2010, 8, 18)
Console.WriteLine(date1.ToString())
' The example displays the following output:
'      8/18/2010 12:00:00 AM      

注解

此构造函数将 yearmonthday 解释为公历的年、月和日。This constructor interprets year, month, and day as a year, month, and day in the Gregorian calendar. 若要使用另一个日历中的年、月和日来实例化 DateTime 值,请调用 @no__t 的构造函数。To instantiate a DateTime value by using the year, month, and day in another calendar, call the DateTime(Int32, Int32, Int32, Calendar) constructor.

生成的 @no__t 的时间为午夜(00:00:00)。The time of day for the resulting DateTime is midnight (00:00:00). Kind 属性初始化为 DateTimeKind.UnspecifiedThe Kind property is initialized to DateTimeKind.Unspecified.

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

DateTime 结构的新实例初始化为指定日历的指定年、月和日。Initializes a new instance of the DateTime structure to the specified year, month, and day for the specified calendar.

public:
 DateTime(int year, int month, int day, System::Globalization::Calendar ^ calendar);
public DateTime (int year, int month, int day, System.Globalization.Calendar calendar);
new DateTime : int * int * int * System.Globalization.Calendar -> DateTime

参数

year
Int32 Int32 Int32 Int32

年(1 到 calendar 中的年数)。The year (1 through the number of years in calendar).

month
Int32 Int32 Int32 Int32

月(1 到 calendar 中的月数)。The month (1 through the number of months in calendar).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

calendar
Calendar Calendar Calendar Calendar

用于解释 yearmonthday 的日历。The calendar that is used to interpret year, month, and day.

异常

year 不在 calendar 支持的范围内。year is not in the range supported by calendar.

- 或 --or- month 小于 1 或大于 calendar 中的月数。month is less than 1 or greater than the number of months in calendar.

-or- day 小于 1 或大于 month 中的天数。day is less than 1 or greater than the number of days in month.

示例

下面的示例调用 @no__t 的构造函数两次,以实例化两个 @no__t 1 值。The following example calls the DateTime(Int32, Int32, Int32, Calendar) constructor twice to instantiate two DateTime values. 第一次调用通过使用 @no__t 的对象实例化 @no__t 值0。The first call instantiates a DateTime value by using a PersianCalendar object. 由于不能将波斯历指定为区域性的默认日历,因此在波斯历中显示日期要求对其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonth 和 @no__t 2 方法进行单独调用。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. 对构造函数的第二次调用通过使用 @no__t 的对象实例化 @no__t 值0。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. 由于回历是当前区域性的默认日历,@no__t 0 方法使用它来设置日期的格式。Because Hijri is the current culture's default calendar, the Console.WriteLine method uses it to format the date. 在还原以前的当前区域性(在此示例中为英语(美国))时,@no__t 方法使用当前区域性的默认公历来设置日期格式。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.

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
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Threading

Module Example
   Public Sub Main()
      Console.WriteLine("Using the Persian Calendar:")
      Dim persian As New PersianCalendar()
      Dim date1 As New Date(1389, 5, 27, persian)
      Console.WriteLine(date1.ToString())
      Console.WriteLine("{0}/{1}/{2}", persian.GetMonth(date1), _
                                       persian.GetDayOfMonth(date1), _
                                       persian.GetYear(date1))
      Console.WriteLine()
      
      Console.WriteLine("Using the Hijri Calendar:")
      ' Get current culture so it can later be restored.
      Dim dftCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
      
      ' Define Hijri calendar.
      Dim hijri As New HijriCalendar()
      ' Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-SY")
      Dim current As CultureInfo = CultureInfo.CurrentCulture
      current.DateTimeFormat.Calendar = hijri
      Dim dFormat As String = current.DateTimeFormat.ShortDatePattern
      ' Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy")
      current.DateTimeFormat.ShortDatePattern = dFormat
      Dim date2 As New Date(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) 
   End Sub
   
   Private Function GetCalendarName(cal As Calendar) As String
      Return Regex.Match(cal.ToString(), "\.(\w+)Calendar").Groups(1).Value
   End Function
End Module
' 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

注解

生成的 @no__t 的时间为午夜(00:00:00)。The time of day for the resulting DateTime is midnight (00:00:00). Kind 属性初始化为 UnspecifiedThe Kind property is initialized to Unspecified.

@No__t-0、`month` 和 day 的允许值取决于 calendarThe allowable values for year, month, and day depend on calendar. 如果无法使用 @no__t 表示指定的日期和时间,则会引发异常。An exception is thrown if the specified date and time cannot be expressed using calendar.

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。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. 请参阅 Handling a new era in the Japanese calendar in .NET(使用 .NET 处理日语日历中的新元号),了解详细信息并确定是否会影响你的应用程序。See Handling a new era in the Japanese calendar in .NET for more information and to determine whether your applications are affected. 请参阅针对日语元号更改准备应用程序,了解有关在 Windows 系统上测试应用程序以确保针对元号更改的准备情况信息。See Prepare your application for the Japanese era change for information on testing your applications on Windows systems to ensure their readiness for the era change. 有关 .Net 中支持多个年号日历的功能,以及在使用支持多个年号的日历时的最佳做法,请参阅使用年号See Working with eras for features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras.

@No__t 的命名空间提供了多个日历,包括 GregorianCalendarJulianCalendarThe System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

另请参阅

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

DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟和秒。Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

public:
 DateTime(int year, int month, int day, int hour, int minute, int second);
public DateTime (int year, int month, int day, int hour, int minute, int second);
new DateTime : int * int * int * int * int * int -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer)

参数

year
Int32 Int32 Int32 Int32

年(1 到 9999)。The year (1 through 9999).

month
Int32 Int32 Int32 Int32

月(1 到 12)。The month (1 through 12).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

小时(0 到 23)。The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

分(0 到 59)。The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

秒(0 到 59)。The seconds (0 through 59).

异常

year 小于 1 或大于 9999。year is less than 1 or greater than 9999.

-or- month 小于 1 或大于 12。month is less than 1 or greater than 12.

-or- day 小于 1 或大于 month中的天数。day is less than 1 or greater than the number of days in month.

-or- hour 小于 0 或大于 23。hour is less than 0 or greater than 23.

-or- minute 小于 0 或大于 59。minute is less than 0 or greater than 59.

-or- second 小于 0 或大于 59。second is less than 0 or greater than 59.

示例

下面的示例使用 DateTime 构造函数来实例化 @no__t 1 值。The following example uses the DateTime constructor to instantiate a DateTime value.

DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 0);
Console.WriteLine(date1.ToString());
// The example displays the following output:
//      8/18/2010 4:32:00 PM
Dim date1 As New Date(2010, 8, 18, 16, 32, 0)
Console.WriteLine(date1.ToString())
' The example displays the following output:
'      8/18/2010 4:32:00 PM

注解

Kind 属性初始化为 UnspecifiedThe Kind property is initialized to Unspecified.

此构造函数解释 @ no__t-0、month,并 day 作为公历年、月和日。This constructor interpretsyear, month, and day as a year, month, and day in the Gregorian calendar. 若要使用另一个日历中的年、月和日来实例化 DateTime 值,请调用 @no__t 的构造函数。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.

如果应用程序的日期和时间数据的可移植性或有限程度的时区感知非常重要,则可以使用相应的 DateTimeOffset 构造函数。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.

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

DateTime 结构的新实例初始化为指定年、月、日、小时、分钟、秒和协调世界时 (UTC) 或本地时间。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.

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
public DateTime (int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * DateTimeKind -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, kind As DateTimeKind)

参数

year
Int32 Int32 Int32 Int32

年(1 到 9999)。The year (1 through 9999).

month
Int32 Int32 Int32 Int32

月(1 到 12)。The month (1 through 12).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

小时(0 到 23)。The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

分(0 到 59)。The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

秒(0 到 59)。The seconds (0 through 59).

kind
DateTimeKind DateTimeKind DateTimeKind DateTimeKind

枚举值之一,该值指示 yearmonthdayhourminutesecond 指定了本地时间、协调世界时 (UTC),还是两者皆未指定。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.

异常

year 小于 1 或大于 9999。year is less than 1 or greater than 9999.

-or- month 小于 1 或大于 12。month is less than 1 or greater than 12.

- 或 --or- day 小于 1 或大于 month中的天数。day is less than 1 or greater than the number of days in month.

-or- hour 小于 0 或大于 23。hour is less than 0 or greater than 23.

-or- minute 小于 0 或大于 59。minute is less than 0 or greater than 59.

- 或 --or- second 小于 0 或大于 59。second is less than 0 or greater than 59.

kind 不是 DateTimeKind 值之一。kind is not one of the DateTimeKind values.

示例

下面的示例使用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) 构造函数来实例化 @no__t 1 值。The following example uses the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) constructor to instantiate a DateTime value.

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:
//      8/18/2010 4:32:00 PM Local
Dim date1 As New Date(2010, 8, 18, 16, 32, 0, DateTimeKind.Local)
Console.WriteLine("{0} {1}", date1, date1.Kind)
' The example displays the following output:
'      8/18/2010 4:32:00 PM Local

注解

此构造函数解释 @ no__t-0、month,并 day 作为公历年、月和日。This constructor interpretsyear, month, and day as a year, month, and day in the Gregorian calendar. 若要使用另一个日历中的年、月和日来实例化 DateTime 值,请调用 @no__t 的构造函数。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.

如果应用程序的日期和时间数据的可移植性或有限程度的时区感知非常重要,则可以使用相应的 DateTimeOffset 构造函数。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.

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

DateTime 结构的新实例初始化为指定日历的年、月、日、小时、分钟和秒。Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second for the specified calendar.

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, System::Globalization::Calendar ^ calendar);
public DateTime (int year, int month, int day, int hour, int minute, int second, System.Globalization.Calendar calendar);
new DateTime : int * int * int * int * int * int * System.Globalization.Calendar -> DateTime

参数

year
Int32 Int32 Int32 Int32

年(1 到 calendar 中的年数)。The year (1 through the number of years in calendar).

month
Int32 Int32 Int32 Int32

月(1 到 calendar 中的月数)。The month (1 through the number of months in calendar).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

小时(0 到 23)。The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

分(0 到 59)。The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

秒(0 到 59)。The seconds (0 through 59).

calendar
Calendar Calendar Calendar Calendar

用于解释 yearmonthday 的日历。The calendar that is used to interpret year, month, and day.

异常

year 不在 calendar 支持的范围内。year is not in the range supported by calendar.

- 或 --or- month 小于 1 或大于 calendar 中的月数。month is less than 1 or greater than the number of months in calendar.

- 或 --or- day 小于 1 或大于 month中的天数。day is less than 1 or greater than the number of days in month.

-or- hour 小于 0 或大于 23hour is less than 0 or greater than 23 -or- minute 小于 0 或大于 59。minute is less than 0 or greater than 59.

-or- second 小于 0 或大于 59。second is less than 0 or greater than 59.

示例

下面的示例调用 @no__t 的构造函数两次,以实例化两个 @no__t 1 值。The following example calls the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) constructor twice to instantiate two DateTime values. 第一次调用通过使用 @no__t 的对象实例化 @no__t 值0。The first call instantiates a DateTime value by using a PersianCalendar object. 由于不能将波斯历指定为区域性的默认日历,因此在波斯历中显示日期要求对其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonth 和 @no__t 2 方法进行单独调用。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. 对构造函数的第二次调用通过使用 @no__t 的对象实例化 @no__t 值0。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. 由于回历是当前区域性的默认日历,@no__t 0 方法使用它来设置日期的格式。Because Hijri is the current culture's default calendar, the Console.WriteLine method uses it to format the date. 在还原以前的当前区域性(在此示例中为英语(美国))时,@no__t 方法使用当前区域性的默认公历来设置日期格式。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.

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
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Threading

Module Example
   Public Sub Main()
      Console.WriteLine("Using the Persian Calendar:")
      Dim persian As New PersianCalendar()
      Dim date1 As New Date(1389, 5, 27, 16, 32, 0, persian)
      Console.WriteLine(date1.ToString())
      Console.WriteLine("{0}/{1}/{2} {3}{6}{4:D2}{6}{5:D2}", persian.GetMonth(date1), _
                                       persian.GetDayOfMonth(date1), _
                                       persian.GetYear(date1), _
                                       persian.GetHour(date1), _
                                       persian.GetMinute(date1), _
                                       persian.GetSecond(date1), _
                                       DateTimeFormatInfo.CurrentInfo.TimeSeparator)
      Console.WriteLine()
      
      Console.WriteLine("Using the Hijri Calendar:")
      ' Get current culture so it can later be restored.
      Dim dftCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
      
      ' Define Hijri calendar.
      Dim hijri As New HijriCalendar()
      ' Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-SY")
      Dim current As CultureInfo = CultureInfo.CurrentCulture
      current.DateTimeFormat.Calendar = hijri
      Dim dFormat As String = current.DateTimeFormat.ShortDatePattern
      ' Ensure year is displayed as four digits.
      dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy")
      current.DateTimeFormat.ShortDatePattern = dFormat
      Dim date2 As New Date(1431, 9, 9, 16, 32, 0, 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) 
   End Sub
   
   Private Function GetCalendarName(cal As Calendar) As String
      Return Regex.Match(cal.ToString(), "\.(\w+)Calendar").Groups(1).Value
   End Function
End Module
' 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 属性初始化为 UnspecifiedThe Kind property is initialized to Unspecified.

@No__t-0、`month` 和 day 的允许值取决于 calendarThe allowable values for year, month, and day depend on calendar. 如果无法使用 @no__t 表示指定的日期和时间,则会引发异常。An exception is thrown if the specified date and time cannot be expressed using calendar.

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。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. 请参阅 Handling a new era in the Japanese calendar in .NET(使用 .NET 处理日语日历中的新元号),了解详细信息并确定是否会影响你的应用程序。See Handling a new era in the Japanese calendar in .NET for more information and to determine whether your applications are affected. 请参阅针对日语元号更改准备应用程序,了解有关在 Windows 系统上测试应用程序以确保针对元号更改的准备情况信息。See Prepare your application for the Japanese era change for information on testing your applications on Windows systems to ensure their readiness for the era change. 有关 .Net 中支持多个年号日历的功能,以及在使用支持多个年号的日历时的最佳做法,请参阅使用年号See Working with eras for features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras.

@No__t 的命名空间提供了多个日历,包括 GregorianCalendarJulianCalendarThe System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

另请参阅

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

DateTime 结构的新实例初始化为指定的年、月、日、小时、分钟、秒和毫秒。Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond.

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond);
new DateTime : int * int * int * int * int * int * int -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer)

参数

year
Int32 Int32 Int32 Int32

年(1 到 9999)。The year (1 through 9999).

month
Int32 Int32 Int32 Int32

月(1 到 12)。The month (1 through 12).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

小时(0 到 23)。The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

分(0 到 59)。The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

秒(0 到 59)。The seconds (0 through 59).

millisecond
Int32 Int32 Int32 Int32

毫秒(0 到 999)。The milliseconds (0 through 999).

异常

year 小于 1 或大于 9999。year is less than 1 or greater than 9999.

-or- month 小于 1 或大于 12。month is less than 1 or greater than 12.

- 或 --or- day 小于 1 或大于 month中的天数。day is less than 1 or greater than the number of days in month.

- 或 --or- hour 小于 0 或大于 23。hour is less than 0 or greater than 23.

-or- minute 小于 0 或大于 59。minute is less than 0 or greater than 59.

-or- second 小于 0 或大于 59。second is less than 0 or greater than 59.

- 或 --or- millisecond 小于 0 或大于 999。millisecond is less than 0 or greater than 999.

示例

下面的示例使用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32) 构造函数来实例化 @no__t 1 值。The following example uses the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32) constructor to instantiate a DateTime value.

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:
//      8/18/2010 4:32:18.500 PM
Dim date1 As New Date(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:
'      8/18/2010 4:32:18.500 PM

注解

此构造函数解释 @ no__t-0、month,并 day 作为公历年、月和日。This constructor interpretsyear, month, and day as a year, month, and day in the Gregorian calendar. 若要使用另一个日历中的年、月和日来实例化 DateTime 值,请调用 @no__t 的构造函数。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.

Kind 属性初始化为 UnspecifiedThe Kind property is initialized to Unspecified.

如果应用程序的日期和时间数据的可移植性或有限程度的时区感知非常重要,则可以使用相应的 DateTimeOffset 构造函数。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.

另请参阅

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

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

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * int * DateTimeKind -> DateTime
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, kind As DateTimeKind)

参数

year
Int32 Int32 Int32 Int32

年(1 到 9999)。The year (1 through 9999).

month
Int32 Int32 Int32 Int32

月(1 到 12)。The month (1 through 12).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

小时(0 到 23)。The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

分(0 到 59)。The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

秒(0 到 59)。The seconds (0 through 59).

millisecond
Int32 Int32 Int32 Int32

毫秒(0 到 999)。The milliseconds (0 through 999).

kind
DateTimeKind DateTimeKind DateTimeKind DateTimeKind

枚举值之一,该值指示 yearmonthday, hourminutesecondmillisecond 指定了本地时间、协调世界时 (UTC),还是两者皆未指定。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.

异常

year 小于 1 或大于 9999。year is less than 1 or greater than 9999.

-or- month 小于 1 或大于 12。month is less than 1 or greater than 12.

-or- day 小于 1 或大于 month中的天数。day is less than 1 or greater than the number of days in month.

- 或 --or- hour 小于 0 或大于 23。hour is less than 0 or greater than 23.

- 或 --or- minute 小于 0 或大于 59。minute is less than 0 or greater than 59.

- 或 --or- second 小于 0 或大于 59。second is less than 0 or greater than 59.

-or- millisecond 小于 0 或大于 999。millisecond is less than 0 or greater than 999.

kind 不是 DateTimeKind 值之一。kind is not one of the DateTimeKind values.

示例

下面的示例使用 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) 构造函数来实例化 @no__t 1 值。The following example uses the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind) constructor to instantiate a DateTime value.

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:
//      8/18/2010 4:32:18.500 PM Local
Dim date1 As New Date(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:
'      8/18/2010 4:32:18.500 PM Local

注解

此构造函数解释 @ no__t-0、month,并 day 作为公历年、月和日。This constructor interpretsyear, month, and day as a year, month, and day in the Gregorian calendar. 若要使用另一个日历中的年、月和日来实例化 DateTime 值,请调用 @no__t 的构造函数。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.

如果应用程序的日期和时间数据的可移植性或有限程度的时区感知非常重要,则可以使用相应的 DateTimeOffset 构造函数。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.

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

DateTime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒和毫秒。Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, second, and millisecond for the specified calendar.

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, System::Globalization::Calendar ^ calendar);
public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar);
new DateTime : int * int * int * int * int * int * int * System.Globalization.Calendar -> DateTime

参数

year
Int32 Int32 Int32 Int32

年(1 到 calendar 中的年数)。The year (1 through the number of years in calendar).

month
Int32 Int32 Int32 Int32

月(1 到 calendar 中的月数)。The month (1 through the number of months in calendar).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

小时(0 到 23)。The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

分(0 到 59)。The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

秒(0 到 59)。The seconds (0 through 59).

millisecond
Int32 Int32 Int32 Int32

毫秒(0 到 999)。The milliseconds (0 through 999).

calendar
Calendar Calendar Calendar Calendar

用于解释 yearmonthday 的日历。The calendar that is used to interpret year, month, and day.

异常

year 不在 calendar 支持的范围内。year is not in the range supported by calendar.

-or- month 小于 1 或大于 calendar 中的月数。month is less than 1 or greater than the number of months in calendar.

-or- day 小于 1 或大于 month中的天数。day is less than 1 or greater than the number of days in month.

-or- hour 小于 0 或大于 23。hour is less than 0 or greater than 23.

-or- minute 小于 0 或大于 59。minute is less than 0 or greater than 59.

-or- second 小于 0 或大于 59。second is less than 0 or greater than 59.

-or- millisecond 小于 0 或大于 999。millisecond is less than 0 or greater than 999.

示例

下面的示例调用 @no__t 的构造函数两次,以实例化两个 @no__t 1 值。The following example calls the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) constructor twice to instantiate two DateTime values. 第一次调用通过使用 @no__t 的对象实例化 @no__t 值0。The first call instantiates a DateTime value by using a PersianCalendar object. 由于不能将波斯历指定为区域性的默认日历,因此在波斯历中显示日期要求对其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonth 和 @no__t 2 方法进行单独调用。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. 对构造函数的第二次调用通过使用 @no__t 的对象实例化 @no__t 值0。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. 由于回历是当前区域性的默认日历,@no__t 0 方法使用它来设置日期的格式。Because Hijri is the current culture's default calendar, the Console.WriteLine method uses it to format the date. 在还原以前的当前区域性(在此示例中为英语(美国))时,@no__t 方法使用当前区域性的默认公历来设置日期格式。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.

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
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Threading

Module Example
   Public Sub Main()
      Console.WriteLine("Using the Persian Calendar:")
      Dim persian As New PersianCalendar()
      Dim date1 As New Date(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}", _
                                       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()
      
      Console.WriteLine("Using the Hijri Calendar:")
      ' Get current culture so it can later be restored.
      Dim dftCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
      
      ' Define strings for use in composite formatting.
      Dim dFormat As String 
      Dim fmtString As String 
      ' Define Hijri calendar.
      Dim hijri As New HijriCalendar()
      ' Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-SY")
      Dim current As CultureInfo = 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 + "}"
      Dim date2 As New Date(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) 
   End Sub
   
   Private Function GetCalendarName(cal As Calendar) As String
      Return Regex.Match(cal.ToString(), "\.(\w+)Calendar").Groups(1).Value
   End Function
End Module
' The example displays the following output:
'       Using the Persian Calendar:
'       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 属性初始化为 UnspecifiedThe Kind property is initialized to Unspecified.

@No__t-0、`month` 和 day 的允许值取决于 calendarThe allowable values for year, month, and day depend on calendar. 如果无法使用 @no__t 表示指定的日期和时间,则会引发异常。An exception is thrown if the specified date and time cannot be expressed using calendar.

如果应用程序的日期和时间数据的可移植性或有限程度的时区感知非常重要,则可以使用相应的 DateTimeOffset 构造函数。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.

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。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. 请参阅 Handling a new era in the Japanese calendar in .NET(使用 .NET 处理日语日历中的新元号),了解详细信息并确定是否会影响你的应用程序。See Handling a new era in the Japanese calendar in .NET for more information and to determine whether your applications are affected. 请参阅针对日语元号更改准备应用程序,了解有关在 Windows 系统上测试应用程序以确保针对元号更改的准备情况信息。See Prepare your application for the Japanese era change for information on testing your applications on Windows systems to ensure their readiness for the era change. 有关 .Net 中支持多个年号日历的功能,以及在使用支持多个年号的日历时的最佳做法,请参阅使用年号See Working with eras for features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras.

@No__t 的命名空间提供了多个日历,包括 GregorianCalendarJulianCalendarThe System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

另请参阅

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

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

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, System::Globalization::Calendar ^ calendar, DateTimeKind kind);
public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * int * System.Globalization.Calendar * DateTimeKind -> DateTime

参数

year
Int32 Int32 Int32 Int32

年(1 到 calendar 中的年数)。The year (1 through the number of years in calendar).

month
Int32 Int32 Int32 Int32

月(1 到 calendar 中的月数)。The month (1 through the number of months in calendar).

day
Int32 Int32 Int32 Int32

日(1 到 month 中的天数)。The day (1 through the number of days in month).

hour
Int32 Int32 Int32 Int32

小时(0 到 23)。The hours (0 through 23).

minute
Int32 Int32 Int32 Int32

分(0 到 59)。The minutes (0 through 59).

second
Int32 Int32 Int32 Int32

秒(0 到 59)。The seconds (0 through 59).

millisecond
Int32 Int32 Int32 Int32

毫秒(0 到 999)。The milliseconds (0 through 999).

calendar
Calendar Calendar Calendar Calendar

用于解释 yearmonthday 的日历。The calendar that is used to interpret year, month, and day.

kind
DateTimeKind DateTimeKind DateTimeKind DateTimeKind

枚举值之一,该值指示 yearmonthday, hourminutesecondmillisecond 指定了本地时间、协调世界时 (UTC),还是两者皆未指定。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.

异常

year 不在 calendar 支持的范围内。year is not in the range supported by calendar.

-or- month 小于 1 或大于 calendar 中的月数。month is less than 1 or greater than the number of months in calendar.

- 或 --or- day 小于 1 或大于 month中的天数。day is less than 1 or greater than the number of days in month.

-or- hour 小于 0 或大于 23。hour is less than 0 or greater than 23.

-or- minute 小于 0 或大于 59。minute is less than 0 or greater than 59.

-or- second 小于 0 或大于 59。second is less than 0 or greater than 59.

-or- millisecond 小于 0 或大于 999。millisecond is less than 0 or greater than 999.

kind 不是 DateTimeKind 值之一。kind is not one of the DateTimeKind values.

示例

下面的示例调用 @no__t 的构造函数两次,以实例化两个 @no__t 1 值。The following example calls the DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) constructor twice to instantiate two DateTime values. 第一次调用通过使用 @no__t 的对象实例化 @no__t 值0。The first call instantiates a DateTime value by using a PersianCalendar object. 由于不能将波斯历指定为区域性的默认日历,因此在波斯历中显示日期要求对其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonth 和 @no__t 2 方法进行单独调用。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. 对构造函数的第二次调用通过使用 @no__t 的对象实例化 @no__t 值0。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. 由于回历是当前区域性的默认日历,@no__t 0 方法使用它来设置日期的格式。Because Hijri is the current culture's default calendar, the Console.WriteLine method uses it to format the date. 在还原以前的当前区域性(在此示例中为英语(美国))时,@no__t 方法使用当前区域性的默认公历来设置日期格式。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.

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
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports System.Threading

Module Example
   Public Sub Main()
      Console.WriteLine("Using the Persian Calendar:")
      Dim persian As New PersianCalendar()
      Dim date1 As New Date(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}", _
                                       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()
      
      Console.WriteLine("Using the Hijri Calendar:")
      ' Get current culture so it can later be restored.
      Dim dftCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
      
      ' Define strings for use in composite formatting.
      Dim dFormat As String 
      Dim fmtString As String 
      ' Define Hijri calendar.
      Dim hijri As New HijriCalendar()
      ' Make ar-SY the current culture and Hijri the current calendar.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-SY")
      Dim current As CultureInfo = 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}"
      Dim date2 As New Date(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) 
   End Sub
   
   Private Function GetCalendarName(cal As Calendar) As String
      Return Regex.Match(cal.ToString(), "\.(\w+)Calendar").Groups(1).Value
   End Function
End Module
' The example displays the following output:
'       Using the Persian Calendar:
'       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

注解

@No__t-0、`month` 和 @no__t 2 参数的允许值取决于 calendar 参数。The allowable values for year, month, and day parameters depend on the calendar parameter. 如果无法使用 @no__t 表示指定的日期和时间,则会引发异常。An exception is thrown if the specified date and time cannot be expressed using calendar.

如果应用程序的日期和时间数据的可移植性或有限程度的时区感知非常重要,则可以使用相应的 DateTimeOffset 构造函数。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.

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。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. 请参阅 Handling a new era in the Japanese calendar in .NET(使用 .NET 处理日语日历中的新元号),了解详细信息并确定是否会影响你的应用程序。See Handling a new era in the Japanese calendar in .NET for more information and to determine whether your applications are affected. 请参阅针对日语元号更改准备应用程序,了解有关在 Windows 系统上测试应用程序以确保针对元号更改的准备情况信息。See Prepare your application for the Japanese era change for information on testing your applications on Windows systems to ensure their readiness for the era change. 有关 .Net 中支持多个年号日历的功能,以及在使用支持多个年号的日历时的最佳做法,请参阅使用年号See Working with eras for features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras.

@No__t 的命名空间提供了多个日历,包括 GregorianCalendarJulianCalendarThe System.Globalization namespace provides several calendars including GregorianCalendar and JulianCalendar.

适用于