1

My mvc angular web app is hosted in West-Europe region on Microsoft Azure and date time field is stored in UTC/GMT format in sql server. I need to convert this date time to uk date time and show on the front end to the users. My angular mark-up looks like

<div class="col-md-9">
  {{resource.CreatedOn |  date:'dd/MM/yyyy HH:mm' : 'UTC' }} <span class="pull-right">{{resource.CreatedBy}}</span>
</div>

The date time shows fine when testing locally E.g. 2016-04-14 09:13:28.723 is the UTC date-time in the database. My server side code converts it to 2016-04-14 10:13:28.723 (which is the converted uk date time ) and its shows like 14/04/2016 10:13 on the front end to the users.

When the application is deployed to Azure in west europe region its showing date time 14/04/2016 11:13 (UTC +2 hours offset instead of +1 hour). I have checked my model and it does have correct date time before passing to view. When its displayed in the view using angular it changes.

Any ideas how to fix it?

The following extension method converts the UTC date time to UK date time. Its tested and works fine. Its apparently just the front end in Angular js where the issue is occuring.

public static DateTime ConvertUtcDateTimeToLocalDateTime(this DateTime datetime)
    {
        // Need to tell the compiler that this is a UTC time by specifying the kind.
        datetime = DateTime.SpecifyKind(datetime, DateTimeKind.Utc);
        var localTimeZone = new SettingsService().LocalTimeZone;
        // Get the system time zone for the local time zone e.g. "GMT Standard Time" in this case
        var localSystemTimeZone = TimeZoneInfo.FindSystemTimeZoneById(localTimeZone);
        return TimeZoneInfo.ConvertTime(datetime, localSystemTimeZone);
    }

This is how the UTC time is created and then saved in the sql server db

entity.CreatedOn = DateTime.UtcNow;

rumi
  • 3,293
  • 12
  • 68
  • 109
  • Post the relevant code. The database doesn't change the stored values. Either the server-side code or Javascript ignores DST. You can avoid such errors by using date types with offsets, eg `DateTimeOffset` and `datetimeoffset`. This has nothing to do with Azure and probably nothing to do with the database - unless a query is made using local time instead of UTC values, eg as filters – Panagiotis Kanavos Apr 14 '16 at 13:20
  • @PanagiotisKanavos please see the the updated question. The dates usage has grown to a point where we can't change the date type. – rumi Apr 14 '16 at 13:27
  • This *is* a DST issue. `GMT Standard Time` isn't UK time. It doesn't adjust for DST (check [this](http://stackoverflow.com/questions/2292334/difference-between-utc-and-gmt-standard-time-in-net) question) so it's the same as UTC. I'll have to check, but I'd bet the method returns the original UTC values as if they were local. Therefore, diffferent servers will format them differently, according to their timezone. Why don't you leave the value as UTC? – Panagiotis Kanavos Apr 14 '16 at 13:36
  • The UTC time from database advances two hours ahead by the method above instead of one hour and this is what is happening at the moment on the azure server. When tested locally it advances to 1 hour as required. – rumi Apr 14 '16 at 13:46
  • Sorry, can't reproduce. I'm at +3, DateTime.Now returns 17:00 and calling this function returns 18:00 which is correct. UTC 17:00 is BST 18:00. Which is why I always use DateTimeOffset. The *database* doesn't change any values, it simply returns them as they are with Kind=Unspecified. It's the code that causes the conversions. – Panagiotis Kanavos Apr 14 '16 at 14:02
  • Logged server's details and they are `server date time now: 14/04/2016 15:12:37- server date time kind: Local - server date time current zone: Coordinated Universal Time- does server supports DST: True` – rumi Apr 14 '16 at 15:17
  • I guess this kind of confirms that the issue is related to angular? – rumi Apr 14 '16 at 15:24

0 Answers0