Cloud Zone is brought to you in partnership with:

I am developing Windows Store and Windows Phone apps, using all kind of Microsoft technologies to provide unique user experiences. I am also using Xamarin for Android/iOS and Microsoft Azure for all cloud things. I am sharing my experiences with others to help and spread knowledge. Marco is a DZone MVB and is not an employee of DZone and has posted 37 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Productive with WAMS: Respect the Time Zone Offset for Every User

08.13.2013
| 2111 views |
  • submit to reddit

In this second post about Windows Azure Mobile Services (WAMS) I will show you how to respect the time zone of every user.

If you have users that are from all over the world, they all have different time zones. Your Mobile Services script always runs at UTC time, and every user gets the same date and time if you send them push notifications or update a live tile for example. Users don’t want to calculate the time zone differences, so we need to handle that for them.

UTC time is the time since 01/01/1970 00:00 in milliseconds. If we know this, it is somewhat easy to show users their local date and time.

Let’s have a look at the Windows Phone code.

To get the local time zone in our Windows Phone app, we only need three lines of code:

TimeZoneInfo localZone = TimeZoneInfo.Local;
DateTime localTime = DateTime.Now;
TimeSpan offsetToUTC = localZone.GetUtcOffset(localTime);

As you can see, we are getting the local time zone first. This is essential because this one is UTC-based. Then we are creating a TimeSpan on our actual DateTime object to get the offset. To get this TimeSpan working on our Azure Mobile Service, which uses JavaScript, we need to convert it to milliseconds. That is the value that has an equal value on all programming languages.

useritemLookUp.TimezoneOffset = offsetToUTC.TotalMilliseconds;

This is the final line of code, which is used to update our user’s item in our SQL table row (for example).

Let’s have a look at the Azure code.

The code is similar to our Windows Phone code. First, we need to fetch the time zone offset from our SQL table:

var sql = "select * from users";

mssql.query(sql, {
        success: function (results) {
            if (results.length > 0) {
                for (var i = 0; i < results.length; i++) {
                    userResult = {
                                        TimeZoneOffset: results[i].TimezoneOffset,
                                        }

TimeZoneOffset = userResult.TimeZoneOffset;

This way, we can run through our whole table on an Azure script and calculate the correct time, which is pretty easy to achieve:

var d = new Date();
var locald = new Date(d.getTime() + TimeZoneOffset);

These two lines generate the local time in milliseconds for the specific user entry. You don’t have to worry whether a user is before or after UTC, it will always calculate the correct time.

If you want to use it, for example, to show the updated time to your users, you can format the time like I described in this post.

That’s all about respecting local time for your users with Windows Azure and Windows Phone.

Happy coding everyone!


Published at DZone with permission of Marco Siccardi, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)