Andre's Blog

Personal blog of Andre Perusse

Silverlight: Displaying UTC Date Values in Local Time

I've recently started working on an existing project that's using Silverlight for the UI. One of the first things I ran up against was displaying DateTime values that come from the database in UTC format. I want to display these values in the user's local time zone, not as UTC. The DateTime object in .NET has methods that allow for the conversion of UTC to local time and back, but I didn't want to write a translation layer between the objects (entities) coming from the remoting call and the actual Silverlight UI. So, what to do?

Thankfully, Google is my friend. I quickly found some source code for a ValueConverter that does just this. Check out this forum post for the details: http://forums.silverlight.net/t/206312.aspx/1

After I created this class in a separate project, all I had to do was add this ValueConverter to the App.xaml's <Application.Resources> element, and I was good to go. Well, almost. I'm using Telerik's RadGridView control for Silverlight, so the column in question looks like this:

<telerik:GridViewDataColumn Header="Last Modified" DataMemberBinding="{Binding LastModifiedDate, Converter={StaticResource DateTimeUtc2LocalValueConverter}}" />

The only problem here is that the grid control wants this value converted into an Object and doesn't specify a DateTime as the target type. After a small modification in the ValueConverter code to allow this, I was good to go.