Error DateTimeOffset: UTC offset of local dateTime does not match offset argument

I am trying to create a small method that converts time from one time zone to another. I thought it would be simple enough, but when I breed it, I get this error. The UTC Offset of the local dateTime parameter does not match the offset argument. I assume this is because the server is not in the same time zone as the user who is not helping, as this will be used from all over the world.

 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; } 

Where fromOffset is a number converted to a time interval from the user's time zone, and toZone is the name of the zone into which we are converting. The error on this line is var offset = new DateTimeOffset(inputTime, fromTimeOffset);

Any ideas on how to do this?

+7
c # datetime
source share
2 answers

See the documentation for the reason for the exception:

ArgumentException : dateTime.Kind equals Local and offset not equal to the offset of the local time zone of the system.

The DateTime argument you received has the Kind property set to Local . The easiest way to solve this problem is to set Kind to 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; } 
+14
source share

It uses an extension method that I use to get around this incredibly frustrating solution. See Notes, the best and most efficient way to handle this is to use the DateTimeOffset tick constructor instead of highlighting another intermediate DateTime to change its Kind property.

  /// <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 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)); 
+1
source share

All Articles