DateTimeOffset 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化结构的新实例 DateTimeOffset 。
重载
DateTimeOffset(DateTime)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- 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的值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:
如果值为
< a0/>,则 新实例的属性设置为等于 ,并且属性 设置为等于 。 如果值为DateTime.KindDateTimeKind.Local或 DateTimeKind.Unspecified,DateTime则新实例的属性设置为等于
dateTime,并且该Offset属性设置为等于本地系统的当前时区的偏移量。
另请参阅
适用于
DateTimeOffset(DateTime, TimeSpan)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
DateTimeOffset使用指定的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)
参数
- dateTime
- DateTime
日期和时间。
- offset
- TimeSpan
时间与协调世界时(UTC)的偏移量。
例外
示例
以下示例演示了如何在该时区提前未知时使用日期和时间和本地时区的偏移量初始化 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:
如果值为 KindDateTimeKind.Utc0,则参数的值
offset必须为 0 或 ArgumentException 引发。如果值为 KindDateTimeKind.Local,则参数的值
offset必须等于该特定日期或 ArgumentException 引发的与协调世界时(UTC)的本地时区偏移量。如果值为 Kind , DateTimeKind.Unspecified则
offset参数可以具有任何有效值。
另请参阅
适用于
DateTimeOffset(Int64, TimeSpan)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
DateTimeOffset使用指定的数目ticks和 offset。
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 小时。
示例
以下示例使用任意日期(在本例中,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)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的
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
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的year、、month、day、hourminute、和second来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 小于 1 或大于 9999。
-或-
month 小于 1 或大于 12。
-或-
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)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的year、、month、day、hour、minute、second、 millisecond和offset来初始化结构的新实例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小于一天或大于天数。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)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的、、、、day、minutehour、second、、 millisecond和calendaroffset来初始化结构的新实例DateTimeOffset。 monthyear
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 小于 calendar 参数 MinSupportedDateTime.Year 或大于 MaxSupportedDateTime.Year。
-或-
month中的月份yearcalendar数小于或大于 。
-或-
day小于一天或大于天数。month
-或-
hour 小于零或大于 23。
-或-
minute 小于 0 或大于 59。
-或-
second 小于 0 或大于 59。
-或-
millisecond 小于 0 或大于 999。
-或-
offset 小于 -14 小时或大于 14 小时。
-或-
month参数year不能day表示为日期和时间值。
-或-
该 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、month、day、hourminute、second和millisecond参数都反映由参数指定的calendar日历中表示的值。 如果这些值构成了不能使用此日历表示的日期和时间,则会引发异常。
重要
日本日历中的纪元基于皇帝的统治,因此预计将改变。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar 中标志着令和年号的开始。 这种时代变化会影响使用这些日历的所有应用程序。 有关详细信息以及确定您的应用程序是否受到影响,请参阅 在 .NET 中处理日语日历的新纪元。 有关在 Windows 系统上测试应用程序以确保其为纪元更改做好准备的信息,请参阅 为日本时代更改准备应用程序。 有关支持具有多个纪元的日历以及处理支持多个纪元的日历的最佳做法的 .NET 中的功能,请参阅 “使用纪元”。
另请参阅
适用于
DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, TimeSpan)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的、、、、day、minutehour、second、、 millisecond和microsecondoffset来初始化结构的新实例DateTimeOffset。 monthyear
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。
注解
此构造函数解释yearmonth公day历中的年、月和日。 若要在另一 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
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的year、month、day、millisecondhoursecondminute、microsecond、 和offsetcalendar来初始化结构的新实例。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 小时。
-或-
month参数year不能day表示为日期和时间值。
-或-
该 UtcDateTime 属性早于 MinValue 或晚 MaxValue于 。
注解
此构造函数解释yearmonth公day历中的年、月和日。 若要在另一 DateTimeOffset 个日历中使用年、月和日实例化值,请调用 DateTimeOffset(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, TimeSpan) 构造函数。