UtcDateTime属性返回一个指示为UTC的DateTime值如果偏移量不为它会转换为UTC时间
LocalDateTime属性返回一个指示为Local的DateTime值
这三个属性的在DateTimeOffset中的定义如下代码所示
public DateTime DateTime {
get {
return ClockDateTime;
}
}
public DateTime UtcDateTime {
get {
return DateTimeSpecifyKind(m_dateTime DateTimeKindUtc);
}
}
public DateTime LocalDateTime {
get {
return UtcDateTimeToLocalTime();
}
}
可以看到在LocalDateTime属性中首先会获取UtcDateTime然后调用ToLocalTime()将其转换为本地时间我们现在来看一组测试代码
static void Main(string[] args)
{
DateTimeOffset basic = new DateTimeOffset(
new TimeSpan());
DateTime dateA = basicDateTime;
DateTime dateB = basicLocalDateTime;
DateTime dateC = basicUtcDateTime;
ConsoleWriteLine(basic);
ConsoleWriteLine();
ConsoleWriteLine(Unspecified DateTime: + dateA);
ConsoleWriteLine(Local DateTIme: + dateB);
ConsoleWriteLine(UTC DateTime: + dateC);
}
[] [] [] [] [] [] []