1

A student class has a property called birthday. My web Server is in Beijing (UTC+8), and when I get data from the server in JSON format, the birthday property always reduces 8 hours to the original date, How can I correct this? I am also in Beijing and I add 8 hours to all birthday properties, but if I am in another timezone or the web server is not in Beijing then I have to add other hours manually.

On the web server, here's my code:

    return Json(student);

The codes that get the data:

  var studentReader= new StreamReader(Request.InputStream);
  var student= JsonHelper.FromJson<Student>(studentReader.ReadToEnd());

The JsonHelper class:

   public static T FromJson<T>(this string json)
    {
     return JsonConvert.DeserializeObject<T>(json);
    }
rhashimoto
  • 15,650
  • 2
  • 52
  • 80
Lyly
  • 718
  • 1
  • 8
  • 17

2 Answers2

1

The recommendation is that, you should store all datetime values in UTC. In the client code, you can convert it to any timezone you want.

Thai Anh Duc
  • 524
  • 6
  • 12
  • Can you show me how to convert the Date in to UTC date before return to JSON? Now I read the birhday from database directly and return to JSON.If I am in the UTC+9,am I should add 9 hours? – Lyly May 27 '13 at 16:27
  • There are 2 ways: either: DateTime.ToUniversalTime or TimeZoneInfo.ConvertFromUtc(...) (You can see from one of the above answer. But the most important thing is that, you need to know the timezone of the datetime saved in database. The DateTime value saved in database SHOULD be designed to save in UTC at the first place. There is a good discussion about timezone here: http://stackoverflow.com/questions/1201378/how-does-datetime-touniversaltime-work – Thai Anh Duc May 28 '13 at 01:41
  • Using UTC for any "instant" in time is good (eg "LastModified"). This causes problems however when date represents something which is completely timezone independent (eg birthday or anniversary). Such values should always show the same value, and be independent of end user timezone. – Chris Rogers Sep 27 '13 at 00:57
0

Your JSON serialization is fine. No problems there.

You need to make sure to store all date-time values as UTC. And when you display the dates then you can make the conversion to the Timezone you need.

Here's how I usually do it:

  • Every time I store a DateTime I use DateTime.ToUniversalTime() to make sure the date is stored on UTC on the database.
  • When I need to print the date in a View I have a method that I used that takes a specific TimeZone and converts the date. See bellow for a code example.
  • I never rely on Client-Side zones. If I have a broad range of users from many geographical points I just make then choose their preferred timezone and store this information on their profiles (Google, Microsoft, and 37Signals all do this for their web applications just to name a few examples).

    public static DateTime ToLocalTimeZone(this object date)
    {        
        DateTime dt;
    
        if (!DateTime.TryParse(date.ToString(), out dt))
            return DateTime.UtcNow;
    
        //Timezone string. This can be stored on web.config
        //or on the user profile as well
        var timeZone = "Pacific SA Standard Time"; 
    
        var defaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
        var userTime = TimeZoneInfo.ConvertTimeFromUtc(dt, defaultTimeZone);
    
        return userTime;
    }
    

More info on previous questions here and here

Edit: List of allowed Timezone strings here

Community
  • 1
  • 1
amhed
  • 3,649
  • 2
  • 31
  • 56
  • You means that the server should store the utc time format,and the client when get to birthday it should transfort to it's loacla time format according to its timezime.I check my database,the birthday value like '2013-05-28 00:31:00.000',it seems not like the utc format;If my server time is not utc format, I want to change to time rely on clinet web brower,is it OK?For example,the server is in Beijing(utc+8),If I in utc+8 timezone,I get the data and add 8 hours,If I in utc+7 timezone,I get the data and add 7 hours. – Lyly May 27 '13 at 16:35
  • And another question,If the web server is in Utc+9 timezone like Tokyo,and I am in utc+7 timezone,am I also add 7 hours or 9 hours?As far as I know ,my database's birthday is not utc format and my web server is in Beijing(utc+8),and I am also in Beijing, ervery time I add the birthday from the server Json,I add the timezone(means 8 hours) manually,it is run well! – Lyly May 27 '13 at 16:46
  • Don't rely on the browsers. Most users don't have it setup correctly anyway. Make the user select what timezone he/she is in and store that value on your database for each user profile. Then format the date as each user wants to see it – amhed May 27 '13 at 20:36