Anonymous
Anonymous

Reputation: 9648

How to deal with DateTime and TimeZone in Silverlight?

I am working on Silverlight Application with SQL Azure. As I know if DateTime Kind is Unspecified, Silverlight will change it to Local and it will save to Azure as UTC time.

Everything working as expect but when I get DateTime data from SQL Azure, its Kind is Unspecified and Silverlight does not convert back to Local time when its display on UI.

For example: I use my application in Rome (UTC +1), I set date to 15:00 then save to database. In database it will save as 14:00 (UTC).

Now I open page to edit that data, it query from database as 14:00 with Unspecified kind and it also show 14:00 in page, which obviously incorrect.

I tried to set DateTime kind at DomainService (before return those data to client) by checking if its kind is unspecified, set it to UTC. This time when I change DateTime on my page its kind is always UTC.

It seems like silverlight will convert date only if its kind is Unspecified, otherwise it do nothing, it also can't convert DateTime from database to display properly even those DateTime has Kind.

I want to know, are there any solutions or work around for this problem?

Upvotes: 1

Views: 2650

Answers (1)

Taylor Bird
Taylor Bird

Reputation: 8007

If you know its stored in UTC, have you tried setting datetime, then using having Silverlight explicitly convert to local time? I am not familiar with Silverlight, but in other forms of .NET, you have to do this conversion explicitly for UI operations.

DateTime dt = GetDateFromDB();

dt = dt.SpecifyKind(DateTimeKind.UTC);

DateTime dtAsLocal = dt.ToLocalTime();

Upvotes: 2

Related Questions