2

Basically i have a silverlight .net web application where by the user does his scheduling for his tasks etc. So he chooses a time and date for the task to run, and he could be anywhere in the world, then this time date gets stored in the database however the server that picks up and runs this scheduled tasks will be somewhere else (lets say malta etc) so the time zone will be different. Whats the best possible way without asking the user for the time zone, to convert the users time to the servers time and servers time to user time vice versa in order to process the tasks?

Thanks in advance, Jon

jon
  • 31
  • 2
  • I'm not an expert but I've run into this situation before. I solved it by passing the client timezone as part of the request. – Aviad P. Apr 03 '11 at 13:34

2 Answers2

2

I think you want to store everything on the server in UTC time and then convert it back to the users time when you go to display it in the client application. When the user schedules a task, convert that time to UTC time on the client and pass that to the server. The server then always passes back UTC time to the client.

Here is a quick code example to get the UTC time from the client and convert it to the local time.

//Get UTC time
var utcTime = DateTime.UtcNow;
Console.WriteLine("{0} {1}", utcTime, utcTime.Kind);

//Convert UTC time to local time
var localTime = utcTime.ToLocalTime();
Console.WriteLine("{0} {1}", localTime, localTime.Kind);

The Kind property will tell you if you are dealing with local or UTC time. This SO question also has some good information about converting from UTC to local time.

Community
  • 1
  • 1
rsbarro
  • 27,021
  • 9
  • 71
  • 75
0

>Whats the best possible way without asking the user for the time zone

You are going then to have to assume the clients OS timezone is correctly set (this is often not the case) and use this timezone to convert to/from your servers time which should be in UTC

Here are a couple of extension methods that allow you to do this.

Any dates sent to the client should call ToDisplayTime() before being rendered on the client UI.

Likewise, before the client sends a date to the server it should call FromDisplayTime() to convert it to UTC. thus the server sends and receives times in UTC only

/// <summary>
    /// Converts the specified DateTime (which should be the UTC time) into the users timezone.
    /// </summary>
    /// <param name="utcDateTime"></param>
    /// <returns></returns>
    public static DateTime ToDisplayTime(this DateTime utcDateTime)
    {
        var result = TimeZoneInfo.ConvertTime(utcDateTime, TimeZoneInfo.Local);
        return result;
    }

    /// <summary>
    /// Converts the specified DateTime from local time to UTC
    /// </summary>
    /// <param name="locatDateTime"></param>
    /// <returns></returns>
    public static DateTime FromDisplayTime(this DateTime locatDateTime)
    {
        var result = TimeZoneInfo.ConvertTime(locatDateTime, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById("UTC"));
        return result;
    }
wal
  • 17,409
  • 8
  • 74
  • 109
  • one question does the timezoneinfo class get the users time zone where ever he is situated? does it take the users os timezone? Thank you – jon Apr 03 '11 at 17:43
  • Yes, but as stated, this may cause issues, as assume your user is sitting in Sydney Australia (+10 UTC) and the timezone on their OS is -8, then if your user enters a time it will be different to what they think they're entering by 18hrs! In this case you will have to confirm with the user their timezone is correct. – wal Apr 03 '11 at 23:22