DateTimeOffset.ToOffset(TimeSpan) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将当前 DateTimeOffset 对象的值转换为偏移量值所指定的日期和时间。
public:
DateTimeOffset ToOffset(TimeSpan offset);
public DateTimeOffset ToOffset (TimeSpan offset);
member this.ToOffset : TimeSpan -> DateTimeOffset
Public Function ToOffset (offset As TimeSpan) As DateTimeOffset
参数
- offset
- TimeSpan
DateTimeOffset 值所转换成的偏移量。
返回
一个对象,它等于原始的 DateTimeOffset 对象(也就是说,它们的 ToUniversalTime() 方法返回的时间点相同),但其 Offset 属性设置为 offset
。
例外
生成的 DateTimeOffset 对象的 DateTime 值早于 DateTimeOffset.MinValue。
\- 或 -
生成的 DateTimeOffset 对象的 DateTime 值晚于 DateTimeOffset.MaxValue。
示例
以下示例演示如何使用 ToOffset 方法将 对象DateTimeOffset转换为DateTimeOffset具有不同偏移量的对象。
using System;
public class DateTimeOffsetConversion
{
private static DateTimeOffset sourceTime;
public static void Main()
{
DateTimeOffset targetTime;
sourceTime = new DateTimeOffset(2007, 9, 1, 9, 30, 0,
new TimeSpan(-5, 0, 0));
// Convert to same time (return sourceTime unchanged)
targetTime = sourceTime.ToOffset(new TimeSpan(-5, 0, 0));
ShowDateAndTimeInfo(targetTime);
// Convert to UTC (0 offset)
targetTime = sourceTime.ToOffset(TimeSpan.Zero);
ShowDateAndTimeInfo(targetTime);
// Convert to 8 hours behind UTC
targetTime = sourceTime.ToOffset(new TimeSpan(-8, 0, 0));
ShowDateAndTimeInfo(targetTime);
// Convert to 3 hours ahead of UTC
targetTime = sourceTime.ToOffset(new TimeSpan(3, 0, 0));
ShowDateAndTimeInfo(targetTime);
}
private static void ShowDateAndTimeInfo(DateTimeOffset newTime)
{
Console.WriteLine("{0} converts to {1}", sourceTime, newTime);
Console.WriteLine("{0} and {1} are equal: {2}",
sourceTime, newTime, sourceTime.Equals(newTime));
Console.WriteLine("{0} and {1} are identical: {2}",
sourceTime, newTime,
sourceTime.EqualsExact(newTime));
Console.WriteLine();
}
}
//
// The example displays the following output:
// 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
//
// 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
//
// 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
//
// 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False
open System
let sourceTime = DateTimeOffset(2007, 9, 1, 9, 30, 0, TimeSpan(-5, 0, 0))
let showDateAndTimeInfo newTime =
printfn $"{sourceTime} converts to {newTime}"
printfn $"{sourceTime} and {newTime} are equal: {sourceTime.Equals newTime}"
printfn $"{sourceTime} and {newTime} are identical: {sourceTime.EqualsExact newTime}\n"
[<EntryPoint>]
let main _ =
// Convert to same time (return sourceTime unchanged)
let targetTime = sourceTime.ToOffset(TimeSpan(-5, 0, 0))
showDateAndTimeInfo targetTime
// Convert to UTC (0 offset)
let targetTime = sourceTime.ToOffset TimeSpan.Zero
showDateAndTimeInfo targetTime
// Convert to 8 hours behind UTC
let targetTime = sourceTime.ToOffset(TimeSpan(-8, 0, 0))
showDateAndTimeInfo targetTime
// Convert to 3 hours ahead of UTC
let targetTime = sourceTime.ToOffset(TimeSpan(3, 0, 0))
showDateAndTimeInfo targetTime
0
// The example displays the following output:
// 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
//
// 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
//
// 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
//
// 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
// 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False
Module DateTimeOffsetConversion
Private sourceTime As New DateTimeOffset(#9/1/2007 9:30AM#, _
New TimeSpan(-5, 0, 0))
Public Sub Main()
Dim targetTime As DateTimeOffset
' Convert to same time (return sourceTime unchanged)
targetTime = sourceTime.ToOffset(New TimeSpan(-5, 0, 0))
ShowDateAndTimeInfo(targetTime)
' Convert to UTC (0 offset)
targetTime = sourceTime.ToOffset(TimeSpan.Zero)
ShowDateAndTimeInfo(targetTime)
' Convert to 8 hours behind UTC
targetTime = sourceTime.ToOffset(New TimeSpan(-8, 0, 0))
ShowDateAndTimeInfo(targetTime)
' Convert to 3 hours ahead of UTC
targetTime = sourceTime.ToOffset(New TimeSpan(3, 0, 0))
ShowDateAndTimeInfo(targetTime)
End Sub
Private Sub ShowDateAndTimeInfo(newTime As DateTimeOffset)
Console.WriteLine("{0} converts to {1}", sourceTime, newTime)
Console.WriteLine("{0} and {1} are equal: {2}", _
sourceTime, newTime, sourceTime.Equals(newTime))
Console.WriteLine("{0} and {1} are identical: {2}", _
sourceTime, newTime, _
sourceTime.EqualsExact(newTime))
Console.WriteLine()
End Sub
End Module
'
' The example displays the following output:
' 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
' 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
' 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
'
' 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
' 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
' 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
'
' 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
' 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
' 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
'
' 9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
' 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
' 9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False
注解
方法 ToOffset 是调用 方法的 TimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo) 替代方法。 当时区的偏移量与协调世界时 (UTC) 已知时,它可用于执行从一个时区到另一个时区的简单转换。 但是,由于方法调用返回的原始 DateTimeOffset 对象和新 DateTimeOffset 对象都与特定时区没有明确关联,因此该方法在转换中不应用任何时区调整规则。