How to convert data of date time’s type stored in database to display Time Zone based on different territories?

Introduction

There is a dates type field in the database. When using the field in the report, clients want to convert the fields values based on own Time Zone to show the date field.

Workaround:

Currently, Reporting Services doesnt provide the function that can get the Time Zone of a client machine. To work around the issue, you need to add a custom code in the report to convert Time Zone and create a parameter through which the client users can select his/her Time Zone, and then pass the parameter value to the custom function. Please see the details as follows:

1. Click the Report, select Report Properties and add the custom code as the screenshot shown:

Custom code:

Shared Function FromUTC (ByVal d As Date, ByVal tz As String) As Date
Return (TimeZoneInfo.ConvertTimeBySystemTimeZoneId(d, TimeZoneInfo.Utc.Id, tz))
End Function

2. Create a parameter named TimeZone (you can name the parameter according to your requirement), select Available value and click Specify values.

Label                                                                Value
China Standard Time                                         China Standard Time
Central European Time Zone                              Central European Time Zone
India Time Zone                                                India Time Zone
United States of America Time zones                   United States of America Time zones
                                                                                   

3. Call the custom code and type the expression to convert the Time Zone as follows:
=Code.FromUTC(Fields!UTCDateFromDatabase.Value,Parameters!TimeZone.Value)

Note: If you use the expression =Code.FromUTC(Fields!UTCDateFromDatabase.Value,TimeZone.CurrentTimeZone.StandardName), it cannot achieve the goal because TimeZone.CurrentTimeZone.StandardName gets the TimeZone of Report Server side rather than Client side.

More information:

TimeZone Class
http://msdn.microsoft.com/en-us/library/system.timezone(v=vs.110).aspx

Applies to
Microsoft SQL Server 2005
Microsoft SQL Server 2008
Microsoft SQL Server 2008 R2
Microsoft SQL Server 2012

January 13th, 2014 1:44am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics