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

See the documentation for why the exception is being thrown:

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

The DateTime argument that you receive has its Kind property set to Local. The simplest way around this problem is to set the Kind to Unspecified.

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;

Leave a Comment