我试图创建一个将时间从一个时区转换为另一个时区的小方法。我认为这很简单,但是当我部署它时,我收到此错误The UTC Offset of the local dateTime parameter does not match the offset argument.
我的猜测是因为服务器与用户不在同一时区,因为这样会来自世界各地。
public object ConvertDate(DateTime inputTime, string fromOffset, string toZone)
{
var fromTimeOffset = new TimeSpan(0, - int.Parse(fromOffset), 0);
var to = TimeZoneInfo.FindSystemTimeZoneById(toZone);
var offset = new DateTimeOffset(inputTime, fromTimeOffset);
var destination = TimeZoneInfo.ConvertTime(offset, to);
return destination.DateTime;
}
fromOffset
是一个数字,从用户时区转换为时间跨度,toZone
是我们要转换为的区域的名称。
此行var offset = new DateTimeOffset(inputTime, fromTimeOffset);
有关如何使其发挥作用的任何想法?
答案 0 :(得分:25)
请参阅documentation了解抛出异常的原因:
ArgumentException :
dateTime.Kind
等于Local
,offset
不等于系统本地时区的偏移量。
您收到的DateTime
参数的Kind
属性设置为Local
。解决此问题的最简单方法是将Kind
设置为Undefined
。
public object ConvertDate(DateTime inputTime, string fromOffset, string toZone)
{
// Ensure that the given date and time is not a specific kind.
inputTime = DateTime.SpecifyKind(inputTime, DateTimeKind.Unspecified);
var fromTimeOffset = new TimeSpan(0, - int.Parse(fromOffset), 0);
var to = TimeZoneInfo.FindSystemTimeZoneById(toZone);
var offset = new DateTimeOffset(inputTime, fromTimeOffset);
var destination = TimeZoneInfo.ConvertTime(offset, to);
return destination.DateTime;
}
答案 1 :(得分:2)
这是我用来解决这个令人难以置信的令人沮丧的框架决策的扩展方法。请参阅注释,处理此问题的最佳和最高效的方法是使用DateTimeOffset
的ticks构造函数,而不必仅分配另一个中间DateTime
来更改它Kind
属性。
/// <summary>
/// Converts a DateTime to a DateTimeOffset, without risking any onerous exceptions
/// the framework quite unfortunately throws within the DateTimeOffset constructor,
/// such as they do when the source DateTime's Kind is not set to UTC. The best and
/// most performant way around this, which we do herein, is to simply construct the
/// new DateTimeOffset with the overload that excepts Ticks. Also, we will simply
/// return <see cref="DateTimeOffset.MinValue"/> if the source DateTime was
/// <see cref="DateTime.MinValue"/>.
/// </summary>
/// <param name="dt">Source DateTime.</param>
/// <param name="offset">Offset</param>
public static DateTimeOffset ToDateTimeOffset(this DateTime dt, TimeSpan offset)
{
// adding negative offset to a min-datetime will throw, this is a
// sufficient catch. Note however that a DateTime of just a few hours can still throw
if (dt == DateTime.MinValue)
return DateTimeOffset.MinValue;
return new DateTimeOffset(dt.Ticks, offset);
}
public static DateTimeOffset ToDateTimeOffset(this DateTime dt, double offsetInHours = 0)
=> ToDateTimeOffset(dt, offsetInHours == 0 ? TimeSpan.Zero : TimeSpan.FromHours(offsetInHours));