实例化 DateTimeOffset 对象
DateTimeOffset 结构提供了多种创建新 DateTimeOffset 值的方法。 许多方法与可实例化新 DateTime 值的方法直接对应,其中有一些改进,允许指定日期和时间值与协调世界时 (UTC) 之间的时差。 尤其是,可通过以下方法实例化 DateTimeOffset 值:
通过使用日期和时间文本。
通过调用 DateTimeOffset 构造函数。
将一个值隐式转换为 DateTimeOffset 值。
分析日期和时间的字符串表示形式。
本主题提供了更多详细信息和代码示例,演示了这些实例化新 DateTimeOffset 值的方法。
日期和时间文本
对于支持它的语言,实例化 DateTime 值最常用的方法之一是将日期和时间作为硬编码文本值提供。 例如,下面的 Visual Basic 代码创建了一个 DateTime 对象,其值是 2008 年 5 月 1 日上午 8:06:32。
Dim literalDate1 As Date = #05/01/2008 8:06:32 AM#
Console.WriteLine(literalDate1.ToString())
' Displays:
' 5/1/2008 8:06:32 AM
使用支持 DateTime 文本的语言时,还可使用日期和时间文本初始化 DateTimeOffset 值。 例如,下面的 Visual Basic 代码创建了一个 DateTimeOffset 对象。
Dim literalDate As DateTimeOffset = #05/01/2008 8:06:32 AM#
Console.WriteLine(literalDate.ToString())
' Displays:
' 5/1/2008 8:06:32 AM -07:00
如控制台输出所示,向通过此方法创建的 DateTimeOffset 值分配了本地时区的时差。 这意味着,如果该代码在其他计算机上运行,则使用字符文本分配的 DateTimeOffset 值不会识别单个时间点。
DateTimeOffset 构造函数
DateTimeOffset 类型定义了六个构造函数。 其中四个直接对应 DateTime 构造函数,其包含可定义与 UTC 之间的日期和时间的时差的类型 TimeSpan 的额外参数。 这使得能够以其单独的日期和时间成分为基础定义 DateTimeOffset 值。 例如,下面的代码使用这四个构造函数来实例化 DateTimeOffset 对象,这些对象具有相同的值 2008/5/1 8:06:32 +01:00。
DateTimeOffset dateAndTime;
// Instantiate date and time using years, months, days,
// hours, minutes, and seconds
dateAndTime = new DateTimeOffset(2008, 5, 1, 8, 6, 32,
new TimeSpan(1, 0, 0));
Console.WriteLine(dateAndTime);
// Instantiate date and time using years, months, days,
// hours, minutes, seconds, and milliseconds
dateAndTime = new DateTimeOffset(2008, 5, 1, 8, 6, 32, 545,
new TimeSpan(1, 0, 0));
Console.WriteLine("{0} {1}", dateAndTime.ToString("G"),
dateAndTime.ToString("zzz"));
// Instantiate date and time using Persian calendar with years,
// months, days, hours, minutes, seconds, and milliseconds
dateAndTime = new DateTimeOffset(1387, 2, 12, 8, 6, 32, 545,
new PersianCalendar(),
new TimeSpan(1, 0, 0));
// Note that the console output displays the date in the Gregorian
// calendar, not the Persian calendar.
Console.WriteLine("{0} {1}", dateAndTime.ToString("G"),
dateAndTime.ToString("zzz"));
// Instantiate date and time using number of ticks
// 05/01/2008 8:06:32 AM is 633,452,259,920,000,000 ticks
dateAndTime = new DateTimeOffset(633452259920000000, new TimeSpan(1, 0, 0));
Console.WriteLine(dateAndTime);
// The example displays the following output to the console:
// 5/1/2008 8:06:32 AM +01:00
// 5/1/2008 8:06:32 AM +01:00
// 5/1/2008 8:06:32 AM +01:00
// 5/1/2008 8:06:32 AM +01:00
Dim dateAndTime As DateTimeOffset
' Instantiate date and time using years, months, days,
' hours, minutes, and seconds
dateAndTime = New DateTimeOffset(2008, 5, 1, 8, 6, 32, _
New TimeSpan(1, 0, 0))
Console.WriteLine(dateAndTime)
' Instantiate date and time using years, months, days,
' hours, minutes, seconds, and milliseconds
dateAndTime = New DateTimeOffset(2008, 5, 1, 8, 6, 32, 545, _
New TimeSpan(1, 0, 0))
Console.WriteLine("{0} {1}", dateAndTime.ToString("G"), _
dateAndTime.ToString("zzz"))
' Instantiate date and time using Persian calendar with years,
' months, days, hours, minutes, seconds, and milliseconds
dateAndTime = New DateTimeOffset(1387, 2, 12, 8, 6, 32, 545, New PersianCalendar, New TimeSpan(1, 0, 0))
' Note that the console output displays the date in the Gregorian
' calendar, not the Persian calendar.
Console.WriteLine("{0} {1}", dateAndTime.ToString("G"), _
dateAndTime.ToString("zzz"))
' Instantiate date and time using number of ticks
' 05/01/2008 8:06:32 AM is 633,452,259,920,000,000 ticks
dateAndTime = New DateTimeOffset(633452259920000000, New TimeSpan(1, 0, 0))
Console.WriteLine(dateAndTime)
' The example displays the following output to the console:
' 5/1/2008 8:06:32 AM +01:00
' 5/1/2008 8:06:32 AM +01:00
' 5/1/2008 8:06:32 AM +01:00
' 5/1/2008 8:06:32 AM +01:00
请注意,当使用 PersianCalendar 对象作为构造函数的其中一个参数而实例化的 DateTimeOffset 对象的值显示到控制台时,它将表示为公历日期,而非波斯历日期。 要使用伊朗历输出日期,请参阅 PersianCalendar 主题中的示例。
其他两个构造函数从 DateTime 值创建 DateTimeOffset 对象。 其中第一个具有单个参数:要转换为 DateTimeOffset 值的 DateTime 值。 得到的 DateTimeOffset 值的时差取决于构造函数的单个参数的 Kind 属性。 如果其值为 DateTimeKind.Utc,则时差设置为 TimeSpan.Zero。 否则,会将其时差设置为等于本地时区的时差。 下面的示例阐释了如何使用此构造函数实例化表示 UTC 和本地时区的 DateTimeOffset 对象:
// Declare date; Kind property is DateTimeKind.Unspecified
DateTime sourceDate = new DateTime(2008, 5, 1, 8, 30, 0);
DateTimeOffset targetTime;
// Instantiate a DateTimeOffset value from a UTC time
DateTime utcTime = DateTime.SpecifyKind(sourceDate, DateTimeKind.Utc);
targetTime = new DateTimeOffset(utcTime);
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM +00:00
// Because the Kind property is DateTimeKind.Utc,
// the offset is TimeSpan.Zero.
// Instantiate a DateTimeOffset value from a UTC time with a zero offset
targetTime = new DateTimeOffset(utcTime, TimeSpan.Zero);
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM +00:00
// Because the Kind property is DateTimeKind.Utc,
// the call to the constructor succeeds
// Instantiate a DateTimeOffset value from a UTC time with a negative offset
try
{
targetTime = new DateTimeOffset(utcTime, new TimeSpan(-2, 0, 0));
Console.WriteLine(targetTime);
}
catch (ArgumentException)
{
Console.WriteLine("Attempt to create DateTimeOffset value from {0} failed.",
targetTime);
}
// Throws exception and displays the following to the console:
// Attempt to create DateTimeOffset value from 5/1/2008 8:30:00 AM +00:00 failed.
// Instantiate a DateTimeOffset value from a local time
DateTime localTime = DateTime.SpecifyKind(sourceDate, DateTimeKind.Local);
targetTime = new DateTimeOffset(localTime);
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM -07:00
// Because the Kind property is DateTimeKind.Local,
// the offset is that of the local time zone.
// Instantiate a DateTimeOffset value from an unspecified time
targetTime = new DateTimeOffset(sourceDate);
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM -07:00
// Because the Kind property is DateTimeKind.Unspecified,
// the offset is that of the local time zone.
' Declare date; Kind property is DateTimeKind.Unspecified
Dim sourceDate As Date = #5/1/2008 8:30 AM#
Dim targetTime As DateTimeOffset
' Instantiate a DateTimeOffset value from a UTC time
Dim utcTime As Date = Date.SpecifyKind(sourceDate, DateTimeKind.Utc)
targetTime = New DateTimeOffset(utcTime)
Console.WriteLine(targetTime)
' Displays 5/1/2008 8:30:00 AM +00:00
' Because the Kind property is DateTimeKind.Utc,
' the offset is TimeSpan.Zero.
' Instantiate a DateTimeOffset value from a local time
Dim localTime As Date = Date.SpecifyKind(sourceDate, DateTimeKind.Local)
targetTime = New DateTimeOffset(localTime)
Console.WriteLine(targetTime)
' Displays 5/1/2008 8:30:00 AM -07:00
' Because the Kind property is DateTimeKind.Local,
' the offset is that of the local time zone.
' Instantiate a DateTimeOffset value from an unspecified time
targetTime = New DateTimeOffset(sourceDate)
Console.WriteLine(targetTime)
' Displays 5/1/2008 8:30:00 AM -07:00
' Because the Kind property is DateTimeKind.Unspecified,
' the offset is that of the local time zone.
'
注意
调用拥有单个 DateTime 参数的 DateTimeOffset 构造函数的重载相当于将 DateTime 值隐式转换为 DateTimeOffset 值。
从 DateTime 值创建 DateTimeOffset 对象的第二个构造函数拥有两个参数:要转换的 DateTime 值,以及表示日期和时间与 UTC 之间的时差的 TimeSpan 值。 此时差值必须与构造函数第一个参数的 Kind 属性相对应,否则会引发 ArgumentException。 如果第一个参数的 Kind 属性为 DateTimeKind.Utc则第二个参数的值必须为 TimeSpan.Zero。 如果第一个参数的 Kind 属性为 DateTimeKind.Local,那么第二个参数的值必须为本地系统时区的时差。 如果第一个参数的 Kind 属性为 DateTimeKind.Unspecified,那么时差可以是任意有效值。 下面的代码说明调用此构造函数将 DateTime 转换为 DateTimeOffset 值。
DateTime sourceDate = new DateTime(2008, 5, 1, 8, 30, 0);
DateTimeOffset targetTime;
// Instantiate a DateTimeOffset value from a UTC time with a zero offset.
DateTime utcTime = DateTime.SpecifyKind(sourceDate, DateTimeKind.Utc);
targetTime = new DateTimeOffset(utcTime, TimeSpan.Zero);
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM +00:00
// Because the Kind property is DateTimeKind.Utc,
// the call to the constructor succeeds
// Instantiate a DateTimeOffset value from a UTC time with a non-zero offset.
try
{
targetTime = new DateTimeOffset(utcTime, new TimeSpan(-2, 0, 0));
Console.WriteLine(targetTime);
}
catch (ArgumentException)
{
Console.WriteLine("Attempt to create DateTimeOffset value from {0} failed.",
utcTime);
}
// Throws exception and displays the following to the console:
// Attempt to create DateTimeOffset value from 5/1/2008 8:30:00 AM failed.
// Instantiate a DateTimeOffset value from a local time with
// the offset of the local time zone
DateTime localTime = DateTime.SpecifyKind(sourceDate, DateTimeKind.Local);
targetTime = new DateTimeOffset(localTime,
TimeZoneInfo.Local.GetUtcOffset(localTime));
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM -07:00
// Because the Kind property is DateTimeKind.Local and the offset matches
// that of the local time zone, the call to the constructor succeeds.
// Instantiate a DateTimeOffset value from a local time with a zero offset.
try
{
targetTime = new DateTimeOffset(localTime, TimeSpan.Zero);
Console.WriteLine(targetTime);
}
catch (ArgumentException)
{
Console.WriteLine("Attempt to create DateTimeOffset value from {0} failed.",
localTime);
}
// Throws exception and displays the following to the console:
// Attempt to create DateTimeOffset value from 5/1/2008 8:30:00 AM failed.
// Instantiate a DateTimeOffset value with an arbitrary time zone.
string timeZoneName = "Central Standard Time";
TimeSpan offset = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName).
GetUtcOffset(sourceDate);
targetTime = new DateTimeOffset(sourceDate, offset);
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM -05:00
Dim sourceDate As Date = #5/1/2008 8:30 AM#
Dim targetTime As DateTimeOffset
' Instantiate a DateTimeOffset value from a UTC time with a zero offset.
Dim utcTime As Date = Date.SpecifyKind(sourceDate, DateTimeKind.Utc)
targetTime = New DateTimeOffset(utcTime, TimeSpan.Zero)
Console.WriteLine(targetTime)
' Displays 5/1/2008 8:30:00 AM +00:00
' Because the Kind property is DateTimeKind.Utc,
' the call to the constructor succeeds.
' Instantiate a DateTimeOffset value from a UTC time with a non-zero offset.
Try
targetTime = New DateTimeOffset(utcTime, New TimeSpan(-2, 0, 0))
Console.WriteLine(targetTime)
Catch e As ArgumentException
Console.WriteLine("Attempt to create DateTimeOffset value from {0} failed.", _
utcTime)
End Try
' Throws exception and displays the following to the console:
' Attempt to create DateTimeOffset value from 5/1/2008 8:30:00 AM failed.
' Instantiate a DateTimeOffset value from a local time with
' the offset of the local time zone.
Dim localTime As Date = Date.SpecifyKind(sourceDate, DateTimeKind.Local)
targetTime = New DateTimeOffset(localTime, _
TimeZoneInfo.Local.GetUtcOffset(localTime))
Console.WriteLine(targetTime)
' Because the Kind property is DateTimeKind.Local and the offset matches
' that of the local time zone, the call to the constructor succeeds.
' Instantiate a DateTimeOffset value from a local time with a zero offset.
Try
targetTime = New DateTimeOffset(localTime, TimeSpan.Zero)
Console.WriteLine(targetTime)
Catch e As ArgumentException
Console.WriteLine("Attempt to create DateTimeOffset value from {0} failed.", _
localTime)
End Try
' Throws exception and displays the following to the console:
' Attempt to create DateTimeOffset value from 5/1/2008 8:30:00 AM failed.
' Instantiate a DateTimeOffset value with an arbitrary time zone.
Dim timeZoneName As String = "Central Standard Time"
Dim offset As TimeSpan = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName). _
GetUtcOffset(sourceDate)
targetTime = New DateTimeOffset(sourceDate, offset)
Console.WriteLine(targetTime)
' Displays 5/1/2008 8:30:00 AM -05:00
隐式类型转换
DateTimeOffset 类型支持一种隐式类型转换:从 DateTime 值到 DateTimeOffset 值。 (隐式类型转换是指从一种类型转换为另一种类型,而无需进行显式强制转换(在 C# 中)或转换(在 Visual Basic 中)且不会丢失信息。) 它可以实现如下代码。
DateTimeOffset targetTime;
// The Kind property of sourceDate is DateTimeKind.Unspecified
DateTime sourceDate = new DateTime(2008, 5, 1, 8, 30, 0);
targetTime = sourceDate;
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM -07:00
// define a UTC time (Kind property is DateTimeKind.Utc)
DateTime utcTime = DateTime.SpecifyKind(sourceDate, DateTimeKind.Utc);
targetTime = utcTime;
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM +00:00
// Define a local time (Kind property is DateTimeKind.Local)
DateTime localTime = DateTime.SpecifyKind(sourceDate, DateTimeKind.Local);
targetTime = localTime;
Console.WriteLine(targetTime);
// Displays 5/1/2008 8:30:00 AM -07:00
Dim targetTime As DateTimeOffset
' The Kind property of sourceDate is DateTimeKind.Unspecified
Dim sourceDate As Date = #5/1/2008 8:30 AM#
targetTime = sourceDate
Console.WriteLine(targetTime)
' Displays 5/1/2008 8:30:00 AM -07:00
' define a UTC time (Kind property is DateTimeKind.Utc)
Dim utcTime As Date = Date.SpecifyKind(sourceDate, DateTimeKind.Utc)
targetTime = utcTime
Console.WriteLine(targetTime)
' Displays 5/1/2008 8:30:00 AM +00:00
' Define a local time (Kind property is DateTimeKind.Local)
Dim localTime As Date = Date.SpecifyKind(sourceDate, DateTimeKind.Local)
targetTime = localTime
Console.WriteLine(targetTime)
' Displays 5/1/2008 8:30:00 AM -07:00
生成的 DateTimeOffset 值的时差取决于 DateTime.Kind 属性值。 如果其值为 DateTimeKind.Utc,则时差设置为 TimeSpan.Zero。 如果其值为 DateTimeKind.Local 或 DateTimeKind.Unspecified,会将其时差设置为等于本地时区的时差。
分析日期和时间的字符串表示形式
DateTimeOffset 类型支持四种方法,通过这些方法可将日期和时间的字符串表示形式转换为 DateTimeOffset 值:
Parse:尝试将日期和时间的字符串表示形式转换为 DateTimeOffset 值,如果转换失败,将引发异常。
TryParse:尝试将日期和时间的字符串表示形式转换为 DateTimeOffset 值,如果转换失败,将返回
false
。ParseExact:尝试将指定格式的日期和时间的字符串表示形式转换为 DateTimeOffset 值。 如果转换失败,该方法将引发异常。
TryParseExact:尝试将指定格式的日期和时间的字符串表示形式转换为 DateTimeOffset 值。 如果转换失败,该方法将返回
false
。
下面的示例阐释了对这四种可实例化 DateTimeOffset 值的字符串转换方法的调用。
string timeString;
DateTimeOffset targetTime;
timeString = "05/01/2008 8:30 AM +01:00";
try
{
targetTime = DateTimeOffset.Parse(timeString);
Console.WriteLine(targetTime);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse {0}.", timeString);
}
timeString = "05/01/2008 8:30 AM";
if (DateTimeOffset.TryParse(timeString, out targetTime))
Console.WriteLine(targetTime);
else
Console.WriteLine("Unable to parse {0}.", timeString);
timeString = "Thursday, 01 May 2008 08:30";
try
{
targetTime = DateTimeOffset.ParseExact(timeString, "f",
CultureInfo.InvariantCulture);
Console.WriteLine(targetTime);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse {0}.", timeString);
}
timeString = "Thursday, 01 May 2008 08:30 +02:00";
string formatString;
formatString = CultureInfo.InvariantCulture.DateTimeFormat.LongDatePattern +
" " +
CultureInfo.InvariantCulture.DateTimeFormat.ShortTimePattern +
" zzz";
if (DateTimeOffset.TryParseExact(timeString,
formatString,
CultureInfo.InvariantCulture,
DateTimeStyles.AllowLeadingWhite,
out targetTime))
Console.WriteLine(targetTime);
else
Console.WriteLine("Unable to parse {0}.", timeString);
// The example displays the following output to the console:
// 5/1/2008 8:30:00 AM +01:00
// 5/1/2008 8:30:00 AM -07:00
// 5/1/2008 8:30:00 AM -07:00
// 5/1/2008 8:30:00 AM +02:00
Dim timeString As String
Dim targetTime As DateTimeOffset
timeString = "05/01/2008 8:30 AM +01:00"
Try
targetTime = DateTimeOffset.Parse(timeString)
Console.WriteLine(targetTime)
Catch e As FormatException
Console.WriteLine("Unable to parse {0}.", timeString)
End Try
timeString = "05/01/2008 8:30 AM"
If DateTimeOffset.TryParse(timeString, targetTime) Then
Console.WriteLine(targetTime)
Else
Console.WriteLine("Unable to parse {0}.", timeString)
End If
timeString = "Thursday, 01 May 2008 08:30"
Try
targetTime = DateTimeOffset.ParseExact(timeString, "f", _
CultureInfo.InvariantCulture)
Console.WriteLine(targetTime)
Catch e As FormatException
Console.WriteLine("Unable to parse {0}.", timeString)
End Try
timeString = "Thursday, 01 May 2008 08:30 +02:00"
Dim formatString As String
formatString = CultureInfo.InvariantCulture.DateTimeFormat.LongDatePattern & _
" " & _
CultureInfo.InvariantCulture.DateTimeFormat.ShortTimePattern & _
" zzz"
If DateTimeOffset.TryParseExact(timeString, _
formatString, _
CultureInfo.InvariantCulture, _
DateTimeStyles.AllowLeadingWhite, _
targetTime) Then
Console.WriteLine(targetTime)
Else
Console.WriteLine("Unable to parse {0}.", timeString)
End If
' The example displays the following output to the console:
' 5/1/2008 8:30:00 AM +01:00
' 5/1/2008 8:30:00 AM -07:00
' 5/1/2008 8:30:00 AM -07:00
' 5/1/2008 8:30:00 AM +02:00