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)

使用指定的 、、、monthdaysecondminutehourmillisecondmicrosecondoffset初始化 结构的新实例。DateTimeOffsetyear

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

使用指定的 、、、monthdaysecondminutehourmillisecondmicrosecondoffset初始化 结构的新实例。DateTimeOffsetyear

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.KinddateTime 属性的值:

另请参阅

适用于

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

- 或 -

dateTime.Kind 等于 Localoffset 不等于系统的本地时区偏移量。

- 或 -

未使用整分钟数指定 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

注解

此构造函数的行为在一定程度上取决于 参数的 KinddateTime 属性的值:

另请参阅

适用于

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

- 或 -

monthcalendar 中小于或大于 year 中的月数。

- 或 -

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

注解

yearmonth、、dayhourminutesecond、 和 millisecond 参数都反映参数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

使用指定的 、、、monthdaysecondminutehourmillisecondmicrosecondoffset初始化 结构的新实例。DateTimeOffsetyear

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

使用指定的 、、、 hoursecondmonthmicroseconddaymillisecondminuteoffset初始化 结构的新实例。DateTimeOffsetyear

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) 构造函数。

适用于