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 初始化為指定的 DateOnly ,並 TimeOnly 遵守指定的 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 結構的新執行個體初始化為刻度的指定數目。

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

參數

ticks
Int64

以 0001 年 1 月 1 日 00:00:00.000 (西曆) 以來經過的 100 奈秒間隔數表示的日期和時間。

例外狀況

範例

下列範例示範其中一個建 DateTime 構函式。

// 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.
open System
open System.Globalization

// Create a DateTime for the maximum date and time using ticks.
let dt1 = DateTime DateTime.MaxValue.Ticks

// Create a DateTime for the minimum date and time using ticks.
let 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.
let ticks = DateTime(1979, 07, 28, 22, 35, 5, CultureInfo("en-US", false).Calendar).Ticks
let dt3 = DateTime ticks

printfn $"""1) The maximum date and time is {dt1.ToString "MM-dd/yyyy hh:mm:ss tt"}"""
printfn $"""2) The minimum date and time is {dt2.ToString "MM/dd/yyyy hh:mm:ss tt"}"""
printfn $"""3) The custom  date and time is {dt3.ToString "MM/dd/yyyy hh:mm:ss tt"}"""

printfn $"\nThe custom date and time is created from {ticks:N0} 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 屬性會初始化為 Unspecified

對於日期和時間資料可攜性或有限的時區感知程度很重要的應用程式,您可以使用對應的 DateTimeOffset 建構函式。

適用於

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

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

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System::Globalization::Calendar ^ calendar);
public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar);
new DateTime : int * int * int * int * int * int * int * int * System.Globalization.Calendar -> 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, microsecond As Integer, calendar As 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 建構函式。

適用於

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

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

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind);
public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind);
new DateTime : int * 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, microsecond As Integer, kind As DateTimeKind)

參數

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

指出 yearmonthdayhourminutesecondmillisecond 是指定本地時間或國際標準時間 (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 建構函式。

適用於

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

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

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

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
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, calendar As Calendar, kind As DateTimeKind)

參數

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

指出 yearmonthdayhourminutesecondmillisecond 是指定本地時間或國際標準時間 (UTC),或是兩者皆非的其中一個列舉值。

例外狀況

calendarnull

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 個值。 第一次呼叫會使用 PersianCalendar 物件具 DateTime 現化值。 由於無法將曼文行事曆指定為文化特性的預設行事曆,因此在格文行事曆中顯示日期需要個別呼叫其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonthPersianCalendar.GetYear 方法。 第二次 DateTime 呼叫建構函式會使用 HijriCalendar 物件具現化值。 此範例會將目前文化特性變更為阿拉伯文 (阿拉伯文) ,並將目前文化特性的預設行事曆變更為 Hijri 行事曆。 因為 Hijri 是目前文化特性的預設行事曆,所以 Console.WriteLine 方法會使用它來格式化日期。 當先前的目前文化特性 (,也就是英文 (美國) 還原) 時, Console.WriteLine 此方法會使用目前文化特性的預設西曆來格式化日期。

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

let getCalendarName (cal: Calendar) =
      Regex.Match(string cal, "\\.(\\w+)Calendar").Groups[1].Value

printfn "Using the Persian Calendar:"
let persian = PersianCalendar()
let date1 = DateTime(1389, 5, 27, 16, 32, 18, 500, persian, DateTimeKind.Local)
printfn $"""{date1.ToString "M/dd/yyyy h:mm:ss.fff tt"} {date1.Kind}"""
let sep = DateTimeFormatInfo.CurrentInfo.TimeSeparator
printfn $"{persian.GetMonth date1}/{persian.GetDayOfMonth date1}/{persian.GetYear date1} {persian.GetHour date1}{sep}{persian.GetMinute date1:D2}{sep}{persian.GetSecond date1:D2}.{persian.GetMilliseconds date1:G3} {date1.Kind}\n"

printfn "Using the Hijri Calendar:"
// Get current culture so it can later be restored.
let dftCulture = Thread.CurrentThread.CurrentCulture

// Define Hijri calendar.
let hijri = HijriCalendar()
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture <- CultureInfo "ar-SY"
let current = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar <- hijri

let dFormat = 
    let dFormat = current.DateTimeFormat.ShortDatePattern
    // Ensure year is displayed as four digits.
    Regex.Replace(dFormat, "/yy$", "/yyyy") + " H:mm:ss.fff"

let fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "} {3}"
let date2 = 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
let dFormat2 = DateTimeFormatInfo.CurrentInfo.ShortDatePattern + " H:mm:ss.fff"
let fmtString2 = "{0} culture using the {1} calendar: {2:" + dFormat2 + "} {3}"
Console.WriteLine(fmtString2, CultureInfo.CurrentCulture, getCalendarName CultureInfo.CurrentCulture.Calendar, date2, date2.Kind)


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

備註

monthday 參數的允許值 year 取決於 calendar 參數。 如果指定的日期和時間無法使用 來表示 calendar ,就會擲回例外狀況。

對於日期和時間資料的可攜性或有限程度的時區感知很重要的應用程式,您可以使用對應的 DateTimeOffset 建構函式。

重要

日本曆法的紀元是以天皇的統治為基礎,因此有變更是正常的。 例如,2019 年 5 月 1 日之後,JapaneseCalendarJapaneseLunisolarCalendar 中將開始使用「令和」。 此變更對使用這些日曆的所有應用程式都有影響。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱 在 .NET 的日曆中處理新紀元。 如需在 Windows 系統上測試應用程式以確保其整備時間變更的相關資訊,請參閱 準備您的應用程式以進行日文紀元變更。 如需 .NET 中支援多個紀元的行事曆功能,以及使用支援多個紀元的行事歷時的最佳做法,請參閱 使用紀元

System.Globalization命名空間提供數個行事曆,包括 GregorianCalendarJulianCalendar

適用於

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

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

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond);
public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond);
new DateTime : int * 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, microsecond As Integer)

參數

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 建構函式。

適用於

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

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

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒和毫秒。

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
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, calendar As 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 的行事曆。

例外狀況

calendarnull

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 個值。 第一次呼叫會使用 PersianCalendar 物件具 DateTime 現化值。 由於無法將曼文行事曆指定為文化特性的預設行事曆,因此在格文行事曆中顯示日期需要個別呼叫其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonthPersianCalendar.GetYear 方法。 第二次 DateTime 呼叫建構函式會使用 HijriCalendar 物件具現化值。 此範例會將目前文化特性變更為阿拉伯文 (阿拉伯文) ,並將目前文化特性的預設行事曆變更為 Hijri 行事曆。 因為 Hijri 是目前文化特性的預設行事曆,所以 Console.WriteLine 方法會使用它來格式化日期。 當先前的目前文化特性 (,也就是英文 (美國) 還原) 時, Console.WriteLine 此方法會使用目前文化特性的預設西曆來格式化日期。

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

let getCalendarName (cal: Calendar) =
      Regex.Match(string cal, "\\.(\\w+)Calendar").Groups[1].Value

printfn "Using the Persian Calendar:"
let persian = PersianCalendar()
let date1 = DateTime(1389, 5, 27, 16, 32, 18, 500, persian)
printfn $"""{date1.ToString("M/dd/yyyy h:mm:ss.fff tt")}"""
let sep = DateTimeFormatInfo.CurrentInfo.TimeSeparator
printfn $"{persian.GetMonth date1}/{persian.GetDayOfMonth date1}/{persian.GetYear date1} {persian.GetHour date1}{sep}%02i{persian.GetMinute date1}{sep}%02i{persian.GetSecond date1}.%.3f{persian.GetMilliseconds date1}\n"

printfn "Using the Hijri Calendar:"
// Get current culture so it can later be restored.
let dftCulture = Thread.CurrentThread.CurrentCulture

// Define Hijri calendar.
let hijri = HijriCalendar()
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture <- CultureInfo "ar-SY"
let current = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar <- hijri

let dFormat = 
    let dFormat = current.DateTimeFormat.ShortDatePattern
    // Ensure year is displayed as four digits.
    Regex.Replace(dFormat, "/yy$", "/yyyy") + " H:mm:ss.fff"

let fmtString = "{0} culture using the {1} calendar: {2:" + dFormat + "}"
let date2 = DateTime(1431, 9, 9, 16, 32, 18, 500, hijri)
Console.WriteLine(fmtString, current, getCalendarName hijri, date2)

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


// 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 屬性會初始化為 Unspecified

monthday 的允許值 yearcalendar 依于 。 如果指定的日期和時間無法使用 來表示 calendar ,就會擲回例外狀況。

對於日期和時間資料的可攜性或有限程度的時區感知很重要的應用程式,您可以使用對應的 DateTimeOffset 建構函式。

重要

日本曆法的紀元是以天皇的統治為基礎,因此有變更是正常的。 例如,2019 年 5 月 1 日之後,JapaneseCalendarJapaneseLunisolarCalendar 中將開始使用「令和」。 此變更對使用這些日曆的所有應用程式都有影響。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱 在 .NET 的日曆中處理新紀元。 如需在 Windows 系統上測試應用程式以確保其整備時間變更的相關資訊,請參閱 準備您的應用程式以進行日文紀元變更。 如需 .NET 中支援多個紀元的行事曆功能,以及使用支援多個紀元的行事歷時的最佳做法,請參閱 使用紀元

System.Globalization命名空間提供數個行事曆,包括 GregorianCalendarJulianCalendar

另請參閱

適用於

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

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

DateTime 結構的新執行個體初始化為指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

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

年份 (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

指出 yearmonthdayhourminutesecondmillisecond 是指定本地時間或國際標準時間 (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 值。

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
let date1 = DateTime(2010, 8, 18, 16, 32, 18, 500, DateTimeKind.Local)
printfn $"""{date1.ToString "M/dd/yyyy h:mm:ss.fff tt"} {date1.Kind}"""

// The example displays the following output, in this case for en-us culture:
//      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

備註

這個建構函式會將 yearmonth 、 和 day 解譯為西曆中的年、月和日。 若要在另一 DateTime 個行事曆中使用年、月和日具現化值,請呼叫 建 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 構函式。

對於日期和時間資料的可攜性或有限程度的時區感知很重要的應用程式,您可以使用對應的 DateTimeOffset 建構函式。

適用於

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

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

DateTime 結構的新執行個體初始化為指定的年、月、日、時、分、秒和毫秒。

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

年份 (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 值。

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
let date1 = DateTime(2010, 8, 18, 16, 32, 18, 500)

date1.ToString "M/dd/yyyy h:mm:ss.fff tt"
|> printfn "%s"

// The example displays the following output, in this case for en-us culture:
//      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

備註

這個建構函式會將 yearmonth 、 和 day 解譯為西曆中的年、月和日。 若要在另一 DateTime 個行事曆中使用年、月和日具現化值,請呼叫 建 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 構函式。

Kind 屬性會初始化為 Unspecified

對於日期和時間資料的可攜性或有限程度的時區感知很重要的應用程式,您可以使用對應的 DateTimeOffset 建構函式。

另請參閱

適用於

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

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

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

public:
 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System::Globalization::Calendar ^ calendar, DateTimeKind kind);
public DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, System.Globalization.Calendar calendar, DateTimeKind kind);
new DateTime : int * int * int * int * int * int * int * int * System.Globalization.Calendar * 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, microsecond As Integer, calendar As Calendar, kind As DateTimeKind)

參數

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

指出 yearmonthdayhourminutesecondmillisecond 是指定本地時間或國際標準時間 (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 建構函式。

適用於

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

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

DateTime 結構的新執行個體初始化為指定年、月、日、時、分、秒以及國際標準時間 (UTC) 或本地時間。

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

年份 (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 值。

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
let date1 = DateTime(2010, 8, 18, 16, 32, 0, DateTimeKind.Local)
printfn $"{date1} {date1.Kind}"

// The example displays the following output, in this case for en-us culture:
//      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

備註

這個建構函式會將 yearmonth 、 和 day 解譯為西曆中的年、月和日。 若要使用另一 DateTime 個行事曆中的年、月和日具現化值,請呼叫 建 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind) 構函式。

對於日期和時間資料可攜性或有限的時區感知程度很重要的應用程式,您可以使用對應的 DateTimeOffset 建構函式。

適用於

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

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

DateTime 結構的新執行個體初始化為指定的年、月、日、時、分和秒。

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

年份 (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 值。

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
let date1 = DateTime(2010, 8, 18, 16, 32, 0)
printfn $"{date1}"

// The example displays the following output, in this case for en-us culture:
//      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 屬性會初始化為 Unspecified

這個建構函式會將 yearmonth 、 和 day 解譯為西曆中的年、月和日。 若要使用另一 DateTime 個行事曆中的年、月和日具現化值,請呼叫 建 DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar) 構函式。

對於日期和時間資料可攜性或有限的時區感知程度很重要的應用程式,您可以使用對應的 DateTimeOffset 建構函式。

適用於

DateTime(Int32, Int32, Int32, Calendar)

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

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月和日。

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
Public Sub New (year As Integer, month As Integer, day As Integer, calendar As Calendar)

參數

year
Int32

年 (1 到 calendar 中的年數)。

month
Int32

月 (1 到 calendar 中的月數)。

day
Int32

日 (1 到 month 中的天數)。

calendar
Calendar

用以解譯 yearmonthday 的行事曆。

例外狀況

calendarnull

year 不在 calendar 支援的範圍內。

-或-

month 小於 1 或大於 calendar 中的月數。

-或-

day 小於 1 或大於 month中的天數。

範例

下列範例會呼叫建 DateTime(Int32, Int32, Int32, Calendar) 構函式兩次,以具現化兩 DateTime 個值。 第一次 PersianCalendar 呼叫會使用 物件具現化 DateTime 值。 由於無法將波斯曆指定為文化特性的預設行事曆,因此在波斯曆中顯示日期需要個別呼叫其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonthPersianCalendar.GetYear 方法。 第二次 HijriCalendar 呼叫建構函式會使用 物件具現化 DateTime 值。 此範例會將目前文化特性變更為阿拉伯文 (阿拉伯文) ,並將目前文化特性的預設行事曆變更為回曆。 因為 Hijri 是目前文化特性的預設行事曆, Console.WriteLine 所以 方法會使用它來格式化日期。 當先前的目前文化特性 (,在此案例中為英文 (美國) ) 還原時, Console.WriteLine 此方法會使用目前文化特性的預設西曆來格式化日期。

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

let getCalendarName (cal: Calendar) =
    Regex.Match(string cal, "\\.(\\w+)Calendar").Groups[1].Value

printfn "Using the Persian Calendar:"
let persian = PersianCalendar()
let date1 = DateTime(1389, 5, 27, persian)
printfn $"{date1}"
printfn $"{persian.GetMonth date1}/{persian.GetDayOfMonth date1}/{persian.GetYear date1}\n"

printfn "Using the Hijri Calendar:"
// Get current culture so it can later be restored.
let dftCulture = Thread.CurrentThread.CurrentCulture

// Define Hijri calendar.
let hijri = HijriCalendar()
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture <- CultureInfo "ar-SY"
let current = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar <- hijri

let dFormat =
    let dFormat = current.DateTimeFormat.ShortDatePattern
    // Ensure year is displayed as four digits.
    Regex.Replace(dFormat, "/yy$", "/yyyy")

current.DateTimeFormat.ShortDatePattern <- dFormat
let date2 = DateTime(1431, 9, 9, hijri)
printfn $"{current} culture using the {getCalendarName hijri} calendar: {date2:d}"

// Restore previous culture.
Thread.CurrentThread.CurrentCulture <- dftCulture
printfn $"{CultureInfo.CurrentCulture} culture using the {getCalendarName CultureInfo.CurrentCulture.Calendar} calendar: {date2:d}"


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

備註

產生的 DateTime 當日時間是午夜 (00:00:00) 。 Kind 屬性會初始化為 Unspecified

、 和 的允許值 year 取決於 calendardaymonth 如果指定的日期和時間無法使用 表示,就會擲回例外狀況 calendar

重要

日本曆法的紀元是以天皇的統治為基礎,因此有變更是正常的。 例如,2019 年 5 月 1 日之後,JapaneseCalendarJapaneseLunisolarCalendar 中將開始使用「令和」。 此變更對使用這些日曆的所有應用程式都有影響。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱 在 .NET 中處理日曆的新紀元。 如需在 Windows 系統上測試應用程式以確保其對紀元變更整備的資訊,請參閱 為日文紀元變更準備您的應用程式。 如需支援多個紀元之行事曆的 .NET 功能,以及使用支援多個紀元的行事歷時的最佳做法,請參閱 使用紀元。

System.Globalization命名空間提供數個行事曆,包括 GregorianCalendarJulianCalendar

另請參閱

適用於

DateTime(Int32, Int32, Int32)

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

DateTime 結構的新執行個體初始化為指定的年、月和日。

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

年份 (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) 。

DateTime date1 = new DateTime(2010, 8, 18);
Console.WriteLine(date1.ToString());
// The example displays the following output:
//      8/18/2010 12:00:00 AM
let date1 = DateTime(2010, 8, 18)
printfn $"{date1}"

// 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 解譯為西曆中的年、月和日。 若要使用另一 DateTime 個行事曆中的年、月和日具現化值,請呼叫 建 DateTime(Int32, Int32, Int32, Calendar) 構函式。

產生的 DateTime 當日時間是午夜 (00:00:00) 。 Kind 屬性會初始化為 DateTimeKind.Unspecified

適用於

DateTime(DateOnly, TimeOnly, DateTimeKind)

Source:
DateTime.cs
Source:
DateTime.cs

將 結構的新實例 DateTime 初始化為指定的 DateOnly ,並 TimeOnly 遵守指定的 DateTimeKind

public:
 DateTime(DateOnly date, TimeOnly time, DateTimeKind kind);
public DateTime (DateOnly date, TimeOnly time, DateTimeKind kind);
new DateTime : DateOnly * TimeOnly * DateTimeKind -> DateTime
Public Sub New (date As DateOnly, time As TimeOnly, kind As DateTimeKind)

參數

date
DateOnly

日期部分。

time
TimeOnly

時間部分。

kind
DateTimeKind

其中一個列舉值,指出 datetime 指定當地時間、國際標準時間 (UTC) ,或兩者皆非。

適用於

DateTime(Int64, DateTimeKind)

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

DateTime 結構的新執行個體初始化為指定的刻度數以及國際標準時間 (UTC) 或本地時間。

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

以 0001 年 1 月 1 日 00:00:00.000 (西曆) 以來經過的 100 奈秒間隔數表示的日期和時間。

kind
DateTimeKind

指出 ticks 是指定本地時間或國際標準時間 (UTC),或是兩者皆非的其中一個列舉值。

例外狀況

kind 不是其中一個 DateTimeKind 值。

備註

對於日期和時間資料可攜性或有限的時區感知程度很重要的應用程式,您可以使用對應的 DateTimeOffset 建構函式。

適用於

DateTime(DateOnly, TimeOnly)

Source:
DateTime.cs
Source:
DateTime.cs

將 結構的新實例 DateTime 初始化為指定的 DateOnlyTimeOnly 。 新的實例將會有 Unspecified 種類。

public:
 DateTime(DateOnly date, TimeOnly time);
public DateTime (DateOnly date, TimeOnly time);
new DateTime : DateOnly * TimeOnly -> DateTime
Public Sub New (date As DateOnly, time As TimeOnly)

參數

date
DateOnly

日期部分。

time
TimeOnly

時間部分。

適用於

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

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

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分和秒。

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
Public Sub New (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, calendar As 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 的行事曆。

例外狀況

calendarnull

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 個值。 第一次 PersianCalendar 呼叫會使用 物件具現化 DateTime 值。 由於無法將波斯曆指定為文化特性的預設行事曆,因此在波斯曆中顯示日期需要個別呼叫其 PersianCalendar.GetMonthPersianCalendar.GetDayOfMonthPersianCalendar.GetYear 方法。 第二次 HijriCalendar 呼叫建構函式會使用 物件具現化 DateTime 值。 此範例會將目前文化特性變更為阿拉伯文 (阿拉伯文) ,並將目前文化特性的預設行事曆變更為回曆。 因為 Hijri 是目前文化特性的預設行事曆, Console.WriteLine 所以 方法會使用它來格式化日期。 當先前的目前文化特性 (,在此案例中為英文 (美國) ) 還原時, Console.WriteLine 此方法會使用目前文化特性的預設西曆來格式化日期。

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

let getCalendarName (cal: Calendar) =
    Regex.Match(string cal, "\\.(\\w+)Calendar").Groups[1].Value

printfn "Using the Persian Calendar:"
let persian = PersianCalendar()
let date1 = DateTime(1389, 5, 27, 16, 32, 0, persian)
printfn $"{date1}"
let sep = DateTimeFormatInfo.CurrentInfo.TimeSeparator
printfn $"{persian.GetMonth date1}/{persian.GetDayOfMonth date1}/{persian.GetYear date1} {persian.GetHour date1}{sep}%02i{persian.GetMinute date1}{sep}%02i{persian.GetSecond date1}\n"

printfn "Using the Hijri Calendar:"

// Get current culture so it can later be restored.
let dftCulture = Thread.CurrentThread.CurrentCulture

// Define Hijri calendar.
let hijri = HijriCalendar()

// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture <- CultureInfo "ar-SY"
let current = CultureInfo.CurrentCulture
current.DateTimeFormat.Calendar <- hijri
let dFormat = 
    let dFormat = current.DateTimeFormat.ShortDatePattern
    // Ensure year is displayed as four digits.
    Regex.Replace(dFormat, "/yy$", "/yyyy")

current.DateTimeFormat.ShortDatePattern <- dFormat
let date2 = DateTime(1431, 9, 9, 16, 32, 18, hijri)
printfn $"{current} culture using the {getCalendarName hijri} calendar: {date2:g}"

// Restore previous culture.
Thread.CurrentThread.CurrentCulture <- dftCulture
printfn $"{CultureInfo.CurrentCulture} culture using the {getCalendarName CultureInfo.CurrentCulture.Calendar} calendar: {date2:g}"


// 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 屬性會初始化為 Unspecified

、 和 的允許值 year 取決於 calendardaymonth 如果指定的日期和時間無法使用 表示,就會擲回例外狀況 calendar

重要

日本曆法的紀元是以天皇的統治為基礎,因此有變更是正常的。 例如,2019 年 5 月 1 日之後,JapaneseCalendarJapaneseLunisolarCalendar 中將開始使用「令和」。 此變更對使用這些日曆的所有應用程式都有影響。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱 在 .NET 中處理日曆的新紀元。 如需在 Windows 系統上測試應用程式以確保其對紀元變更整備的資訊,請參閱 為日文紀元變更準備您的應用程式。 如需支援多個紀元之行事曆的 .NET 功能,以及使用支援多個紀元的行事歷時的最佳做法,請參閱 使用紀元。

System.Globalization命名空間提供數個行事曆,包括 GregorianCalendarJulianCalendar

另請參閱

適用於