添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan)

使用指定的 year month day hour minute second millisecond microsecond offset ,初始化 DateTimeOffset 結構的新實例。

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

下列範例說明 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 參數的 DateTime.Kind 屬性值:

  • 如果 DateTime.Kind 的值是 DateTimeKind.Utc,則新實例的 DateTime 屬性會設定為等於 dateTime,且 Offset 屬性設定為等於 Zero

  • 如果 DateTime.Kind 的值是 DateTimeKind.LocalDateTimeKind.Unspecified,則新實例的 DateTime 屬性會設定為等於 dateTime,而 Offset 屬性會設定為等於本機系統目前時區的位移。

    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)

    下列範例示範如何以日期和時間初始化 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 屬性值:

  • 如果 Kind 的值是 DateTimeKind.Utcoffset 參數的值必須是 0,否則會擲回 ArgumentException

  • 如果 Kind 的值是 DateTimeKind.Localoffset 參數的值必須等於該特定日期的當地時區位移,否則會擲回 ArgumentException

  • 如果 Kind 的值是 DateTimeKind.Unspecifiedoffset 參數可以有任何有效的值。

    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)

    下列範例會使用任意日期中的刻度數目來初始化 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
    	
    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)
    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)

    下列範例會使用 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) 建構函式。

    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)

    下列範例會使用 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) 建構函式。

    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)

    下列範例會使用 HebrewCalendar 類別和 HijriCalendar 類別的實例來具現化 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,
                                   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,
                                   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,
                                   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,
                                   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中標示了 Reiwa 時代的開頭。 這種時代變更會影響使用這些行事曆的所有應用程式。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱 處理 .NET 中日曆中的新紀元。 如需在 Windows 系統上測試應用程式,以確保其整備時代變更的相關信息,請參閱 準備您的應用程式以取得日文時代變更。 如需 .NET 中支援具有多個紀元的行事曆功能,以及使用支援多個紀元的行事歷時的最佳作法,請參閱 使用紀元

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