共用方式為


DateTimeOffset 建構函式

定義

初始化結構的新實例 DateTimeOffset

多載

名稱 Description
DateTimeOffset(DateTime)

使用指定的DateTime值初始化結構的新實例DateTimeOffset

DateTimeOffset(DateTime, TimeSpan)

使用指定的DateTime值初始化結構的新實例DateTimeOffsetoffset

DateTimeOffset(Int64, TimeSpan)

使用指定數量ticks的 和 offset初始化結構的新實例DateTimeOffset

DateTimeOffset(DateOnly, TimeOnly, TimeSpan)

使用指定的 datetime、 和 offset初始化結構的新實例DateTimeOffset

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

使用指定的 yearmonthminutehourdaysecond、 及 offset初始化結構的新DateTimeOffset實例。

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

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

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

使用指定的 yearmonthdaycalendarhouroffsetminutemillisecondsecond初始化一個結構的新DateTimeOffset實例。

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

使用指定的 yearmonthdaymicrosecondhouroffsetminutemillisecondsecond初始化一個結構的新DateTimeOffset實例。

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

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

DateTimeOffset(DateTime)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
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的值DateTime.Kind如何影響此建構子回傳的日期與時間值。

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.KinddateTime

另請參閱

適用於

DateTimeOffset(DateTime, TimeSpan)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

使用指定的DateTime值初始化結構的新實例DateTimeOffsetoffset

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 等於 Utcoffset 不等於零。

-或-

dateTime.KindLocal 於且 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

備註

此構造子的行為部分取決於參數性質dateTime的值Kind

另請參閱

適用於

DateTimeOffset(Int64, TimeSpan)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

使用指定數量ticks的 和 offset初始化結構的新實例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

-或-

ticksDateTimeOffset.MinValue.Ticks 於或大於 DateTimeOffset.MaxValue.Ticks

-或-

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

範例

以下範例是利用任意日期(此例為 2007 年 7 月 16 日下午 1:32)的刻數初始化物件 DateTimeOffset ,偏移量為 -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)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

使用指定的 datetime、 和 offset初始化結構的新實例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)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

使用指定的 yearmonthminutehourdaysecond、 及 offset初始化結構的新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 小於一或大於9999。

-或-

month 小於一或大於十二。

-或-

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

-或-

hour 小於零或大於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)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

使用指定的 yearmonthdayhourminutesecondmillisecondoffset初始化結構的新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 小於一或大於9999。

-或-

month 小於一或大於十二。

-或-

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

-或-

hour 小於零或大於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)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

使用指定的 yearmonthdaycalendarhouroffsetminutemillisecondsecond初始化一個結構的新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

用於解釋 yearmonth、 和 day的曆法。

offset
TimeSpan

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

例外狀況

offset 並不代表整分鐘。

calendar 不可以是 null

year 小於參數 calendarMinSupportedDateTime.Year 大於 MaxSupportedDateTime.Year

-或-

month要麼小於或大於 中 的yearcalendar月份數。

-或-

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

-或-

hour 小於零或大於23。

-或-

minute 小於0或大於59。

-或-

second 小於0或大於59。

-或-

millisecond 小於0或大於999。

-或-

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

-或-

year這些、 monthday參數無法以日期和時間值表示。

-或-

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

備註

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

重要

日本日曆中的紀元基於皇帝的統治,因此預計將改變。 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar中標示了 Reiwa 時代的開頭。 這種時代變更會影響使用這些行事曆的所有應用程式。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱 處理 .NET 中日曆中的新紀元。 如需在 Windows 系統上測試應用程式,以確保其準備迎接時代變更的相關信息,請參閱 準備您的應用程式以取得日本年號變更。 如需 .NET 中支援具有多個紀元的行事曆功能,以及使用支援多個紀元的行事曆時的最佳作法,請參閱 使用紀元

另請參閱

適用於

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

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

使用指定的 yearmonthdaymicrosecondhouroffsetminutemillisecondsecond初始化一個結構的新DateTimeOffset實例。

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)

來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs
來源:
DateTimeOffset.cs

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

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

用於解釋 yearmonth、 和 day的曆法。

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小時。

-或-

year這些、 monthday參數無法以日期和時間值表示。

-或-

UtcDateTime 性質早 MinValue 於或晚於 MaxValue

備註

此構造器將 yearmonthday 解釋為格里曆中的年、月與日。 若要在另一個曆法中使用年份、月份和日期來實例化一個 DateTimeOffset 值,請呼叫 DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) 建構子。

適用於