C
C#12mo ago
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;
//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());
}
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());
}
No description
4 Replies
canton7
canton712mo ago
Related tip: .Where(cond).FirstOrDefault() --> .FirstOrDefault(cond)
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX12mo ago
TeBeCo
using System;

var rstTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
var localTime = new TimeOnly(13, 30, 0);

Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 01, 28), localTime , rstTimeZoneInfo ));
Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 03, 28), localTime , rstTimeZoneInfo ));

string GetFormatedFutureLocatedDateTime(DateOnly localDate, TimeOnly localTime, TimeZoneInfo rstTimeZoneInfo)
{
var unknownDateTime = localDate.ToDateTime(localTime);

var rstOffset = rstTimeZoneInfo.GetUtcOffset(unknownDateTime);
var targetDateTimeOffset = new DateTimeOffset(unknownDateTime, rstOffset);

return targetDateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ddK");
}
using System;

var rstTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
var localTime = new TimeOnly(13, 30, 0);

Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 01, 28), localTime , rstTimeZoneInfo ));
Console.WriteLine(GetFormatedFutureLocatedDateTime(new DateOnly(2023, 03, 28), localTime , rstTimeZoneInfo ));

string GetFormatedFutureLocatedDateTime(DateOnly localDate, TimeOnly localTime, TimeZoneInfo rstTimeZoneInfo)
{
var unknownDateTime = localDate.ToDateTime(localTime);

var rstOffset = rstTimeZoneInfo.GetUtcOffset(unknownDateTime);
var targetDateTimeOffset = new DateTimeOffset(unknownDateTime, rstOffset);

return targetDateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ddK");
}
2023-01-28T13:30:28+01:00
2023-03-28T13:30:28+02:00
^
2023-01-28T13:30:28+01:00
2023-03-28T13:30:28+02:00
^
Quoted by
<@689473681302224947> from #web (click here)
React with ❌ to remove this embed.
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server