>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;
}