C#C
C#3y ago
7 replies
Daxand

Persistent error converting to UTC

I'm constantly getting the following error "The conversion could not be completed because the supplied DateTime did not have Kind property set correctly.

However, I've tried defining the Kind to Local and even UTC and it gives the same error.

                            //DateTime toDiff = DateTime.SpecifyKind(DateTime.Today.Add((TimeSpan)Input.To), DateTimeKind.Local);
                            //DateTime toDiff = DateTime.UtcNow.Date.Add((TimeSpan)Input.To);
                            DateTime toDiff = DateTime.UtcNow;
                            DateTime toDiffConverted = Time.ConvertToUtc(toDiff, Input.TimeZone);
                            TimeSpan toCalc = toDiffConverted.TimeOfDay;
                            _company.To = toCalc;

the method being called
     internal static DateTime ConvertToUtc(DateTime time, string userTimezone)
        {
            var timeZonesList = TimeZoneInfo.GetSystemTimeZones();
            var it = timeZonesList.Where(a => a.Id.Equals(userTimezone)).FirstOrDefault();
            return TimeZoneInfo.ConvertTimeToUtc(time, TimeZoneInfo.GetSystemTimeZones().Where(a => a.Id.Equals(userTimezone)).FirstOrDefault());
        }
image.png
Was this page helpful?