6

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"). I would like to display something user-facing like "10:00 AM". Where I am (in England), the clocks have recently gone forward and the following method is an hour out:

var timenowstring = DateTime.UtcNow.ToString("s");
var dateutc = DateTime.Parse(timenowstring).ToShortTimeString();
var datelocal = DateTime.Parse(timenowstring).ToLocalTime().ToShortTimeString();

Console.WriteLine("Utc time string: " + dateutc);
Console.WriteLine("Local time string: " + datelocal);

Both print "9:02 AM" when actually it's 10:02 AM.

Here's a screenshot of it repro-ing on http://csharppad.com/ :

utc and local show the same time - see system clock in lower right, which is correct bigger image

CSharpPad gist

What am I doing wrong and what's the easiest way to get a DateTime object which will return the right time when I call .ToShortTimeString()?

NB the docs on ToLocalTime() say:

The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.

user1002973
  • 2,088
  • 6
  • 22
  • 31
  • possible duplicate of [Why can't DateTime.Parse parse UTC date](http://stackoverflow.com/questions/1756639/why-cant-datetime-parse-parse-utc-date) – rdans Apr 10 '15 at 09:12

3 Answers3

8

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.

CSharpPad Time Zone

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();
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • I’d argue that `dataReader["myDateTimeField"] as DateTime` is a bit unsafe. You *want* an exception thrown if the value of the field turns out not to be `NULL`. You’d rather `var ordinal = dataReader.GetOrdinal("myDateTimeField"); var dt = dataReader.IsDbNull(ordinal) ? new DateTime?() : dataReader.GetDateTime(ordinal);`. That way, if the library has a way to not do a runtime dynamic cast, it will avoid that, and if it isn’t a `DateTime` or `NULL`, you will get an exception pointing out an issue that might be a bug and save yourself the hassle of hunting down strange issues later. – binki Oct 29 '20 at 03:04
1

For others who think DateTime.ToLocalTime is ignoring Daylight Savings time (like I did), something to realize is that the Date you are converting is the basis for whether or not Daylight Savings time is considered.

Example: Converting a UTC DateTime of '1/1/2020' to my local (Central US), I get: '12/31/2019 18:00:00', which is correct. As I was running this in April and Daylight Savings is in effect, I was expecting UTC-5:00 instead of UTC-6:00. But January is CST,not CDT, so UTC-6:00 was correct.

Your problem could be 'user error' as it was mine.

Byron
  • 399
  • 2
  • 12
0

When I run your code locally, it gives expected results (local time corresponds to my machine local time). Although when I run it on http://csharppad.com, times are same. Reason is mentioned in Matt Johnson's answer: Servers have different time zones from local machines.

Bachor
  • 828
  • 12
  • 17