I'm trying to convert a Datetime.Now to UTC -11
TimeZoneInfo info = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x => x.BaseUtcOffset.ToString() == "-11:00:00");
try
{
DateTime localTime = DateTime.Now;
DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(localTime, info);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
The Exception Throwing is: 'The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly. For example, when the Kind property is DateTimeKind.Local, the source time zone must be TimeZoneInfo.Local. Parameter name: sourceTimeZone'
i'm not founding how to fix it with:
localTime = DateTime.SpecifyKind(localTime, DateTimeKind.Local);
nothing changes
DateTimeOffset
instead.DateTime localTime = DateTime.UtcNow; localTime = DateTime.SpecifyKind(localTime, DateTimeKind.Unspecified); DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(localTime, info);
to resolve it. Thanks everyone