1
如果我有一个DateTime实例代表一个有效的UTC时间以及一个将该DateTime转换为它应用的时区的偏移量,那么我该如何构造一个DateTimeOffset实例来表示它呢?DateTimeOffset正确的使用
var utcDateTime = new DateTime(2011, 02, 29, 12, 43, 0, /*DateTimeKind.Utc*/);
var localOffset = TimeSpan.FromHours(2.0);
var dto = ...
// Here the properties should be as follows;
// dto.UtcDateTime = 2011-02-29 12:43:00
// dto.LocalDateTime = 2011-02-29 14:43:00
也许我不正确理解DateTimeOffset结构,但我无法获得预期的输出。
在此先感谢
啊,那很完美。所以DateTimeOffset(DateTime,TimeSpan)构造函数需要一个非UTC日期。这是扔我的LocalDateTime - 使用DateTime属性更有意义! ...另外,2011年是一个错字:P谢谢:) – Barg 2012-02-29 13:19:06