DateTimeOffset 建構函式

定義

初始化 DateTimeOffset 結構的新執行個體。

多載

DateTimeOffset(DateTime)

使用指定的 DateTime 值,初始化 DateTimeOffset 結構的新執行個體。

DateTimeOffset(DateTime, TimeSpan)

使用指定的 DateTime 值及位移,初始化 DateTimeOffset 結構的新執行個體。

DateTimeOffset(Int64, TimeSpan)

使用指定數目的刻度和位移,初始化 DateTimeOffset 結構的新執行個體。

DateTimeOffset(DateOnly, TimeOnly, TimeSpan)

使用指定的 datetimeoffset ,初始化 結構的新實例 DateTimeOffset

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

使用指定的年、月、日、時、分、秒和位移,初始化 DateTimeOffset 結構的新執行個體。

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

使用指定的年、月、日、時、分、秒、毫秒和位移,初始化 DateTimeOffset 結構的新執行個體。

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan)

使用指定之曆法的指定年、月、日、時、分、秒、毫秒和位移,初始化 DateTimeOffset 結構的新執行個體。

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

使用指定的 yearmonthminutemicroseconddayhoursecondmillisecondoffset ,初始化 結構的新實例。 DateTimeOffset

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan)

使用指定的 yearmonthminutemicroseconddayhoursecondmillisecondoffset ,初始化 結構的新實例。 DateTimeOffset

DateTimeOffset(DateTime)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的 DateTime 值,初始化 DateTimeOffset 結構的新執行個體。

public:
 DateTimeOffset(DateTime dateTime);
public DateTimeOffset (DateTime dateTime);
new DateTimeOffset : DateTime -> DateTimeOffset
Public Sub New (dateTime As DateTime)

參數

dateTime
DateTime

日期和時間。

例外狀況

國際標準時間 (UTC) 套用位移所產生的日期和時間早于 DateTimeOffset.MinValue

-或-

套用位移所產生的 UTC 日期和時間晚于 DateTimeOffset.MaxValue

範例

下列範例說明 參數的 DateTime.Kind 屬性值 dateTime 如何影響這個建構函式所傳回的日期和時間值。

DateTime localNow = DateTime.Now;
DateTimeOffset localOffset = new DateTimeOffset(localNow);
Console.WriteLine(localOffset.ToString());

DateTime utcNow = DateTime.UtcNow;
DateTimeOffset utcOffset = new DateTimeOffset(utcNow);
Console.WriteLine(utcOffset.ToString());

DateTime unspecifiedNow = DateTime.SpecifyKind(DateTime.Now,
                               DateTimeKind.Unspecified);
DateTimeOffset unspecifiedOffset = new DateTimeOffset(unspecifiedNow);
Console.WriteLine(unspecifiedOffset.ToString());
//
// The code produces the following output if run on Feb. 23, 2007, on
// a system 8 hours earlier than UTC:
//   2/23/2007 4:21:58 PM -08:00
//   2/24/2007 12:21:58 AM +00:00
//   2/23/2007 4:21:58 PM -08:00
let localNow = DateTime.Now
let localOffset = DateTimeOffset localNow
printfn $"{localOffset}"

let utcNow = DateTime.UtcNow
let utcOffset = DateTimeOffset utcNow
printfn "{utcOffset}"

let unspecifiedNow = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified)
let unspecifiedOffset = DateTimeOffset unspecifiedNow
printfn $"{unspecifiedOffset}"

// The code produces the following output if run on Feb. 23, 2007, on
// a system 8 hours earlier than UTC:
//   2/23/2007 4:21:58 PM -08:00
//   2/24/2007 12:21:58 AM +00:00
//   2/23/2007 4:21:58 PM -08:00
Dim localNow As Date = Date.Now
Dim localOffset As New DateTimeOffset(localNow)
Console.WriteLine(localOffset.ToString())

Dim utcNow As Date = Date.UtcNow
Dim utcOffset As New DateTimeOffset(utcNow)
Console.WriteLine(utcOffset.ToString())

Dim unspecifiedNow As Date = Date.SpecifyKind(Date.Now, _
                                  DateTimeKind.Unspecified)
Dim unspecifiedOffset As New DateTimeOffset(unspecifiedNow)
Console.WriteLine(unspecifiedOffset.ToString())
'
' The code produces the following output if run on Feb. 23, 2007, on
' a system 8 hours earlier than UTC:
'    2/23/2007 4:21:58 PM -08:00
'    2/24/2007 12:21:58 AM +00:00
'    2/23/2007 4:21:58 PM -08:00

備註

此建構函式的行為取決於 DateTime.Kind 參數的 dateTime 屬性值:

另請參閱

適用於

DateTimeOffset(DateTime, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的 DateTime 值及位移,初始化 DateTimeOffset 結構的新執行個體。

public:
 DateTimeOffset(DateTime dateTime, TimeSpan offset);
public DateTimeOffset (DateTime dateTime, TimeSpan offset);
new DateTimeOffset : DateTime * TimeSpan -> DateTimeOffset
Public Sub New (dateTime As DateTime, offset As TimeSpan)

參數

dateTime
DateTime

日期和時間。

offset
TimeSpan

國際標準時間 (UTC) 的時間位移。

例外狀況

dateTime.Kind 等於 Utc,且 offset 不等於零。

-或-

dateTime.Kind 等於 Local,且 offset 不等於該系統的當地時區位移。

-或-

offset 未指定以分鐘為單位。

offset 小於 -14 小時或大於 14 小時。

-或-

UtcDateTime 小於 DateTimeOffset.MinValue 或大於 DateTimeOffset.MaxValue

範例

下列範例示範如何在事先不知道該時區時,使用日期和時間和當地時區的位移來初始化 DateTimeOffset 物件。

DateTime localTime = new DateTime(2007, 07, 12, 06, 32, 00);
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime,
                         TimeZoneInfo.Local.GetUtcOffset(localTime));
Console.WriteLine(dateAndOffset);
// The code produces the following output:
//    7/12/2007 6:32:00 AM -07:00
let localTime = DateTime(2007, 07, 12, 06, 32, 00)
let dateAndOffset = DateTimeOffset(localTime, TimeZoneInfo.Local.GetUtcOffset localTime)
printfn $"{dateAndOffset}"
// The code produces the following output:
//    7/12/2007 6:32:00 AM -07:00
Dim localTime As Date = #07/12/2007 6:32AM#
Dim dateAndOffset As New DateTimeOffset(localTime, _
                         TimeZoneInfo.Local.GetUtcOffset(localTime))
Console.WriteLine(dateAndOffset)
' The code produces the following output:
'    7/12/2007 6:32:00 AM -07:00

備註

此建構函式的行為取決於 Kind 參數的 dateTime 屬性值:

另請參閱

適用於

DateTimeOffset(Int64, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定數目的刻度和位移,初始化 DateTimeOffset 結構的新執行個體。

public:
 DateTimeOffset(long ticks, TimeSpan offset);
public DateTimeOffset (long ticks, TimeSpan offset);
new DateTimeOffset : int64 * TimeSpan -> DateTimeOffset
Public Sub New (ticks As Long, offset As TimeSpan)

參數

ticks
Int64

以 0001 年 1 月 1 日午夜 12:00:00 以來已經過的 100 奈秒間隔數表示的日期和時間。

offset
TimeSpan

國際標準時間 (UTC) 的時間位移。

例外狀況

offset 未指定以分鐘為單位。

屬性 UtcDateTime 早于 DateTimeOffset.MinValue 或晚于 DateTimeOffset.MaxValue

-或-

ticks 小於 DateTimeOffset.MinValue.Ticks 或大於 DateTimeOffset.MaxValue.Ticks

-或-

Offset 小於 -14 小時或大於 14 小時。

範例

下列範例 DateTimeOffset 會在 2007 年 7 月 16 日下午 1:32) ,使用任意 (日期中的刻度數目初始化 物件,其位移為 -5。

DateTime dateWithoutOffset = new DateTime(2007, 7, 16, 13, 32, 00);
DateTimeOffset timeFromTicks = new DateTimeOffset(dateWithoutOffset.Ticks,
                               new TimeSpan(-5, 0, 0));
Console.WriteLine(timeFromTicks.ToString());
// The code produces the following output:
//    7/16/2007 1:32:00 PM -05:00
let dateWithoutOffset = DateTime(2007, 7, 16, 13, 32, 00)
let timeFromTicks = DateTimeOffset(dateWithoutOffset.Ticks, TimeSpan(-5, 0, 0))
printfn $"{timeFromTicks}"
// The code produces the following output:
//    7/16/2007 1:32:00 PM -05:00
Dim dateWithoutOffset As Date = #07/16/2007 1:32PM#
Dim timeFromTicks As New DateTimeOffset(datewithoutOffset.Ticks, _
                         New TimeSpan(-5, 0, 0))
Console.WriteLine(timeFromTicks.ToString())
' The code produces the following output:
'    7/16/2007 1:32:00 PM -05:00

備註

一般而言,嘗試呼叫 DateTimeOffset 建構函式以本機時間具現化 DateTimeOffset 值,而本機時區以外的位移會 ArgumentException 擲回 。 您可以使用這個建構函式的多 DateTimeOffset 載來處理這項限制。 下列範例會使用當地時間的刻度數目來具現化 DateTimeOffset 其位移不一定代表當地時間的值:

DateTime localTime = DateTime.Now;
DateTimeOffset nonLocalDateWithOffset = new DateTimeOffset(localTime.Ticks,
                                  new TimeSpan(2, 0, 0));
Console.WriteLine(nonLocalDateWithOffset);
//
// The code produces the following output if run on Feb. 23, 2007:
//    2/23/2007 4:37:50 PM +02:00
let localTime = DateTime.Now
let nonLocalDateWithOffset = DateTimeOffset(localTime.Ticks, TimeSpan(2, 0, 0))
printfn $"{nonLocalDateWithOffset}"
// The code produces the following output if run on Feb. 23, 2007:
//    2/23/2007 4:37:50 PM +02:00
Dim localTime As Date = Date.Now
Dim nonLocalDateWithOffset As New DateTimeOffset(localTime.Ticks, _
                                  New TimeSpan(2, 0, 0))
Console.WriteLine(nonLocalDateWithOffset)                                        
'
' The code produces the following output if run on Feb. 23, 2007:
'    2/23/2007 4:37:50 PM +02:00

另請參閱

適用於

DateTimeOffset(DateOnly, TimeOnly, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的 datetimeoffset ,初始化 結構的新實例 DateTimeOffset

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

參數

date
DateOnly

日期部分。

time
TimeOnly

時間部分。

offset
TimeSpan

國際標準時間 (UTC) 的時間位移。

適用於

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的年、月、日、時、分、秒和位移,初始化 DateTimeOffset 結構的新執行個體。

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

參數

year
Int32

年份 (1 到 9999)。

month
Int32

月份 (1 到 12)。

day
Int32

日 (1 到 month 中的天數)。

hour
Int32

小時 (0 到 23)。

minute
Int32

分鐘 (0 到 59)。

second
Int32

秒數 (0 到 59)。

offset
TimeSpan

國際標準時間 (UTC) 的時間位移。

例外狀況

offset 不表示整數分鐘數。

year 小於 1 或大於 9999。

-或-

month 小於 1 或大於 12。

-或-

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

-或-

hour 小於 0 或大於 23。

-或-

minute 小於 0 或大於 59。

-或-

second 小於 0 或大於 59。

-或-

offset 小於 -14 小時或大於 14 小時。

-或-

屬性 UtcDateTime 早于 DateTimeOffset.MinValue 或晚于 DateTimeOffset.MaxValue

範例

下列範例會使用 DateTimeOffset.DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) 建構函式多載具 DateTimeOffset 現化 物件。

   DateTime specificDate = new DateTime(2008, 5, 1, 06, 32, 00);
   DateTimeOffset offsetDate = new DateTimeOffset(specificDate.Year,
                                   specificDate.Month,
                                   specificDate.Day,
                                   specificDate.Hour,
                                   specificDate.Minute,
                                   specificDate.Second,
                                   new TimeSpan(-5, 0, 0));
   Console.WriteLine("Current time: {0}", offsetDate);
   Console.WriteLine("Corresponding UTC time: {0}", offsetDate.UtcDateTime);
// The code produces the following output:
//    Current time: 5/1/2008 6:32:00 AM -05:00
//    Corresponding UTC time: 5/1/2008 11:32:00 AM
let specificDate = DateTime(2008, 5, 1, 06, 32, 00)
let offsetDate = DateTimeOffset(specificDate.Year,
                                specificDate.Month,
                                specificDate.Day,
                                specificDate.Hour,
                                specificDate.Minute,
                                specificDate.Second,
                                TimeSpan(-5, 0, 0))
printfn $"Current time: {offsetDate}"
printfn $"Corresponding UTC time: {offsetDate.UtcDateTime}"
// The code produces the following output:
//    Current time: 5/1/2008 6:32:00 AM -05:00
//    Corresponding UTC time: 5/1/2008 11:32:00 AM
   Dim specificDate As Date = #5/1/2008 6:32AM#
   Dim offsetDate As New DateTimeOffset(specificDate.Year, _
                                        specificDate.Month, _
                                        specificDate.Day, _
                                        specificDate.Hour, _
                                        specificDate.Minute, _
                                        specificDate.Second, _
                                        New TimeSpan(-5, 0, 0))
   Console.WriteLine("Current time: {0}", offsetDate)
   Console.WriteLine("Corresponding UTC time: {0}", offsetDate.UtcDateTime)                                              
' The code produces the following output:
'    Current time: 5/1/2008 6:32:00 AM -05:00
'    Corresponding UTC time: 5/1/2008 11:32:00 AM

備註

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

另請參閱

適用於

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的年、月、日、時、分、秒、毫秒和位移,初始化 DateTimeOffset 結構的新執行個體。

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

參數

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

offset
TimeSpan

國際標準時間 (UTC) 的時間位移。

例外狀況

offset 不表示整數分鐘數。

year 小於 1 或大於 9999。

-或-

month 小於 1 或大於 12。

-或-

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

-或-

hour 小於 0 或大於 23。

-或-

minute 小於 0 或大於 59。

-或-

second 小於 0 或大於 59。

-或-

millisecond 小於 0 或大於 999。

-或-

offset 小於 -14 或大於 14。

-或-

屬性 UtcDateTime 早于 DateTimeOffset.MinValue 或晚于 DateTimeOffset.MaxValue

範例

下列範例會使用 DateTimeOffset.DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan) 建構函式多載具 DateTimeOffset 現化 物件。

string fmt = "dd MMM yyyy HH:mm:ss";
DateTime thisDate = new DateTime(2007, 06, 12, 19, 00, 14, 16);
DateTimeOffset offsetDate = new DateTimeOffset(thisDate.Year,
                                               thisDate.Month,
                                               thisDate.Day,
                                               thisDate.Hour,
                                               thisDate.Minute,
                                               thisDate.Second,
                                               thisDate.Millisecond,
                                               new TimeSpan(2, 0, 0));
Console.WriteLine("Current time: {0}:{1}", offsetDate.ToString(fmt), offsetDate.Millisecond);
// The code produces the following output:
//    Current time: 12 Jun 2007 19:00:14:16
let fmt = "dd MMM yyyy HH:mm:ss"
let thisDate = DateTime(2007, 06, 12, 19, 00, 14, 16)
let offsetDate = DateTimeOffset(thisDate.Year,
                                thisDate.Month,
                                thisDate.Day,
                                thisDate.Hour,
                                thisDate.Minute,
                                thisDate.Second,
                                thisDate.Millisecond,
                                TimeSpan(2, 0, 0))
printfn $"Current time: {offsetDate.ToString fmt}:{offsetDate.Millisecond}"
// The code produces the following output:
//    Current time: 12 Jun 2007 19:00:14:16
Dim fmt As String = "dd MMM yyyy HH:mm:ss"
Dim thisDate As DateTime = New Date(2007, 06, 12, 19, 00, 14, 16)
Dim offsetDate As New DateTimeOffset(thisDate.Year, _
                                     thisDate.Month, _
                                     thisDate.Day, _
                                     thisDate.Hour, _
                                     thisDate.Minute, _
                                     thisDate.Second, _
                                     thisDate.Millisecond, _ 
                                     New TimeSpan(2, 0, 0))  
Console.WriteLine("Current time: {0}:{1}", offsetDate.ToString(fmt), _ 
                                           offsetDate.Millisecond)
' The code produces the following output:
'    Current time: 12 Jun 2007 19:00:14:16

備註

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

另請參閱

適用於

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定之曆法的指定年、月、日、時、分、秒、毫秒和位移,初始化 DateTimeOffset 結構的新執行個體。

public:
 DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, System::Globalization::Calendar ^ calendar, TimeSpan offset);
public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, int millisecond, System.Globalization.Calendar calendar, TimeSpan offset);
new DateTimeOffset : int * int * int * int * int * int * int * System.Globalization.Calendar * TimeSpan -> DateTimeOffset
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, offset As TimeSpan)

參數

year
Int32

年。

month
Int32

月份 (1 到 12)。

day
Int32

日 (1 到 month 中的天數)。

hour
Int32

小時 (0 到 23)。

minute
Int32

分鐘 (0 到 59)。

second
Int32

秒數 (0 到 59)。

millisecond
Int32

毫秒 (0 到 999)。

calendar
Calendar

用以解譯 yearmonthday 的行事曆。

offset
TimeSpan

國際標準時間 (UTC) 的時間位移。

例外狀況

offset 不表示整數分鐘數。

calendar 不可以是 null

year 小於 calendar 參數的 MinSupportedDateTime.Year 或大於 MaxSupportedDateTime.Year

-或-

month 小於或大於 calendaryear 的月數。

-或-

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

-或-

hour 小於 0 或大於 23。

-或-

minute 小於 0 或大於 59。

-或-

second 小於 0 或大於 59。

-或-

millisecond 小於 0 或大於 999。

-或-

offset 小於 -14 小時或大於 14 小時。

-或-

yearmonthday 參數無法以日期和時間值表示。

-或-

屬性 UtcDateTime 早于 DateTimeOffset.MinValue 或晚于 DateTimeOffset.MaxValue

範例

下列範例會使用 類別和 HijriCalendar 類別的 HebrewCalendar 實例來具現化 DateTimeOffset 值。 該日期接著會使用個別行事曆和西曆向主控台顯示。

CultureInfo fmt;
int year;
Calendar cal;
DateTimeOffset dateInCal;

// Instantiate DateTimeOffset with Hebrew calendar
year = 5770;
cal = new HebrewCalendar();
fmt = new CultureInfo("he-IL");
fmt.DateTimeFormat.Calendar = cal;
dateInCal = new DateTimeOffset(year, 7, 12,
                               15, 30, 0, 0,
                               cal,
                               new TimeSpan(2, 0, 0));
// Display the date in the Hebrew calendar
Console.WriteLine("Date in Hebrew Calendar: {0:g}",
                   dateInCal.ToString(fmt));
// Display the date in the Gregorian calendar
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal);
Console.WriteLine();

// Instantiate DateTimeOffset with Hijri calendar
year = 1431;
cal = new HijriCalendar();
fmt = new CultureInfo("ar-SA");
fmt.DateTimeFormat.Calendar = cal;
dateInCal = new DateTimeOffset(year, 7, 12,
                               15, 30, 0, 0,
                               cal,
                               new TimeSpan(2, 0, 0));
// Display the date in the Hijri calendar
Console.WriteLine("Date in Hijri Calendar: {0:g}",
                   dateInCal.ToString(fmt));
// Display the date in the Gregorian calendar
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal);
Console.WriteLine();
// Instantiate DateTimeOffset with Hebrew calendar
let year = 5770
let cal = HebrewCalendar()
let fmt = CultureInfo "he-IL"
fmt.DateTimeFormat.Calendar <- cal
let dateInCal = DateTimeOffset(year, 7, 12,
                               15, 30, 0, 0,
                               cal,
                               TimeSpan(2, 0, 0))
// Display the date in the Hebrew calendar
printfn $"Date in Hebrew Calendar: {dateInCal.ToString fmt:g}"
// Display the date in the Gregorian calendar
printfn $"Date in Gregorian Calendar: {dateInCal:g}\n"

// Instantiate DateTimeOffset with Hijri calendar
let year = 1431
let cal = HijriCalendar()
let fmt = CultureInfo "ar-SA"
fmt.DateTimeFormat.Calendar <- cal
let dateInCal = DateTimeOffset(year, 7, 12,
                               15, 30, 0, 0,
                               cal,
                               TimeSpan(2, 0, 0))
// Display the date in the Hijri calendar
printfn $"Date in Hijri Calendar: {dateInCal.ToString fmt:g}"
// Display the date in the Gregorian calendar
printfn $"Date in Gregorian Calendar: {dateInCal:g}\n"
Dim fmt As CultureInfo
Dim year As Integer
Dim cal As Calendar
Dim dateInCal As DateTimeOffset

' Instantiate DateTimeOffset with Hebrew calendar
year = 5770
cal = New HebrewCalendar()
fmt = New CultureInfo("he-IL")
fmt.DateTimeFormat.Calendar = cal      
dateInCal = New DateTimeOffset(year, 7, 12, _
                               15, 30, 0, 0, _
                               cal, _
                               New TimeSpan(2, 0, 0))
' Display the date in the Hebrew calendar
Console.WriteLine("Date in Hebrew Calendar: {0:g}", _
                   dateInCal.ToString(fmt))
' Display the date in the Gregorian calendar                         
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal)
Console.WriteLine()

' Instantiate DateTimeOffset with Hijri calendar
year = 1431
cal = New HijriCalendar()
fmt = New CultureInfo("ar-SA")
fmt.DateTimeFormat.Calendar = cal
dateInCal = New DateTimeOffset(year, 7, 12, _
                               15, 30, 0, 0, _
                               cal, _
                               New TimeSpan(2, 0, 0))
' Display the date in the Hijri calendar
Console.WriteLine("Date in Hijri Calendar: {0:g}", _
                   dateInCal.ToString(fmt))
' Display the date in the Gregorian calendar                         
Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal)
Console.WriteLine()

備註

yearmonthdayhourminutesecondmillisecond 參數都會反映參數所 calendar 指定行事曆中所表示的值。 如果這些值形成無法使用此行事曆表示的日期和時間,就會擲回例外狀況。

重要

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

另請參閱

適用於

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的 year 、、、 daymillisecondsecondhourmicrosecondminuteoffset ,初始化 結構的新實例。 DateTimeOffsetmonth

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

參數

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

offset
TimeSpan

國際標準時間 (UTC) 的時間位移。

例外狀況

offset 不表示整數分鐘數。

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

適用於

DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的 year 、、、 daymillisecondsecondhourmicrosecondminuteoffset ,初始化 結構的新實例。 DateTimeOffsetmonth

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

參數

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

calendar
Calendar

用以解譯 yearmonthday 的行事曆。

offset
TimeSpan

國際標準時間 (UTC) 的時間位移。

例外狀況

offset 不表示整數分鐘數。

year 不在 calendar 支援的範圍內。

-或-

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

-或-

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

-或-

hour 小於 0 或大於 23。

-或-

minute 小於 0 或大於 59。

-或-

second 小於 0 或大於 59。

-或-

millisecond 小於 0 或大於 999。

-或-

microsecond 小於 0 或大於 999。

-或-

offset 小於 -14 小時或大於 14 小時。

-或-

yearmonthday 參數無法以日期和時間值表示。

-或-

UtcDateTime 屬性早於 MinValue 或晚於 MaxValue

備註

此建構函式會將 yearmonthday 解譯為西曆中的年、月和日。 若要在另一 DateTimeOffset 個日曆中使用年、月和日具現化值,請呼叫 建 DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) 構函式。

適用於