Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

datetime

I'm trying to create a small method that converts the time from one timezone to another. I thought it would be simple enough, but when I deploy it I get this error The UTC Offset of the local dateTime parameter does not match the offset argument. My guess is that it's because the server is not in the same timezone as the user which is not helpful since this would be used from around 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 timespan from the users timezone and toZone is the name of the zone we're converting to. The error occurs on this line var offset = new DateTimeOffset(inputTime, fromTimeOffset);

Any ideas on how to get this working?

like image 872
Toxicable Avatar asked Mar 28 '16 04:03

Toxicable


People also ask

How do I convert DateTimeOffset to local time?

In performing the conversion to local time, the method first converts the current DateTimeOffset object's date and time to Coordinated Universal Time (UTC) by subtracting the offset from the time. It then converts the UTC date and time to local time by adding the local time zone offset.

What is offset in DateTimeOffset?

Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

How do I get timezone from UTC offset?

Timezone offset is the time difference in hours or minutes between the Coordinated Universal Time (UTC) and a given time zone. The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time.


1 Answers

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 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; } 
like image 103
reduckted Avatar answered Oct 13 '22 23:10

reduckted