Convert DateTime.Now to another time zone

This code has been working for a long time, but now it has broken when I try to pass DateTime.Now as an outageEndDate parameter:

public Outage(DateTime outageStartDate, DateTime outageEndDate, Dictionary<string, string> weeklyHours, string province, string localProvince) { this.outageStartDate = outageStartDate; this.outageEndDate = outageEndDate; this.weeklyHours = weeklyHours; this.province = province; localTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[localProvince]); if (outageStartDate < outageEndDate) { TimeZoneInfo remoteTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[province]); outageStartDate = TimeZoneInfo.ConvertTime(outageStartDate, localTime, remoteTime); outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime, remoteTime); 

The error message that I get on the last line is that the Kind property is not set correctly in the DateTime (outageEndDate) parameter. I googled Google and checked SO for examples, but I really don't understand the error message.

Any advice is appreciated.

Sincerely.

EDIT - exact error message:

 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 

EDIT: outageEndDate.Kind = Utc

+4
source share
2 answers

Thanks for clarifying your question.

If the DateTime Kind instance is Local , then TimeZoneInfo.ConvertTime expects the second parameter to be the local time zone of your computer.

If the DateTime Kind instance is Utc , then TimeZoneInfo.ConvertTime expects Utc be the second parameter.

You need to first convert outageEndDate to the desired time zone, in case the localProvice time zone does not match the time zone on your computer.

 outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime); 
+8
source

here is an example of what you could try

It depends on what you mean by "GMT + 1 time zone." Do you mean constant UTC + 1, or do you mean UTC + 1 or UTC + 2 depending on DST?

If you are using .NET 3.5, use TimeZoneInfo to get the appropriate time zone, and then use:

 // Store this statically somewhere TimeZoneInfo maltaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("..."); DateTime utc = DateTime.UtcNow; DateTime malta = TimeZoneInfo.ConvertTimeFromUtc(utc, maltaTimeZone ); 

You will need to develop a system identifier for the Malta time zone, but you can do this easily by running this code locally:

 Console.WriteLine(TimeZoneInfo.Local.Id); 

If you use not using .NET 3.5, you yourself need to solve the problem of saving daylight yourself. Honestly, the easiest way to do this is with a simple lookup table. Make DST changes over the next few years, then write a simple method to return the offset at a specific UTC time with this hard code. You may just need a sorted List<DateTime> with known changes and rotate between 1 and 2 hours until your date changes after the last change:

 // Be very careful when building this list, and make sure they're UTC times! private static readonly IEnumerable<DateTime> DstChanges = ...; static DateTime ConvertToLocalTime(DateTime utc) { int hours = 1; // Or 2, depending on the first entry in your list foreach (DateTime dstChange in DstChanges) { if (utc < dstChange) { return DateTime.SpecifyKind(utc.AddHours(hours), DateTimeKind.Local); } hours = 3 - hours; // Alternate between 1 and 2 } throw new ArgumentOutOfRangeException("I don't have enough DST data!"); } 
+1
source

All Articles