You said:
I have a a UTC time string (that I get from a database, so I can't change the format) that is created with DateTime.UtcNow.ToString("s")
.
Right off the bat, you have a problem. Dates in a database are (usually) not stored as strings. They're stored in fields with a specific data type. In SQL Server (for example) you may be using a datetime
or datetime2
field. These are not strings. When you retrieve them into your .NET code, they are converted directly to a DateTime
type. If you are treating it as a string, you are doing it wrong.
For example, your data access code might be doing something like this:
DateTime dt = Convert.ToDateTime(dataReader["myDateTimeField"].ToString());
That is very common, and completely wrong. You should instead be doing this:
DateTime dt = (DateTime) dataReader["myDateTimeField"];
Or if the field is nullable:
DateTime? dt = dataReader["myDateTimeField"] as DateTime;
Once you load the value properly instead of parsing it as a string, the rest will work out fine. The DateTime
value will have DateTimeKind.Unspecified
for its Kind
property, and when you call ToLocalTime
on it, it will assume that you wanted to treat the unspecified value as UTC. (See the chart on MSDN.)
Regarding the code you posted, while it's a bit messy (going through strings unnecessarily), it would actually work just fine - assuming you ran it in your time zone. In the ToLocalTime
method, "local" means the local time zone setting of the machine wherever the code happens to be running. For csharppad.com, the time zone happens to be UTC. It has no way of knowing you want to use England's time zone rules.

If you intend to run your code on a server, then you shouldn't be using ToLocalTime
at all - as the time zone of the server is likely to be irrelevant. Instead, you could use TimeZoneInfo
to convert the time:
// this uses the time zone for England
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTime englandDatetime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, tz);
Alternatively, you could use the open-source Noda Time library, like this:
DateTimeZone tz = DateTimeZoneProviders.Tzdb["Europe/London"];
DateTime englandDateTime = Instant.FromDateTimeUtc(utcDateTime)
.InZone(tz)
.ToDateTimeUnspecified();