Convert Time Zones (ASP.NET MVC)
- 4 minutes to read
Reporting controls allow you to convert UTC date-time values to a local time zone and vice versa with UtcToLocalTime() and LocalTimeToUtc() functions. This is essential for applications with a global user base, as it ensures that all users can analyze report data in their respective local time zones.
This help topic describes how to implement a custom time zone provider and use UtcToLocalTime() and LocalTimeToUtc() functions in your reports.
Implement a Time Zone Provider
Server settings specify the default time zone for your application. To specify a different time zone (for example, the client’s time zone), implement the ITimeZoneProvider interface. The GetCurrentTimeZone() method returns a TimeZoneInfo object that is the local time zone.
The following example implements the ITimeZoneProvider interface. In this example, the time zone is retrieved from a cookie named LocalTimeZone.
Create a class that implements the
ITimeZoneProviderinterface:using DevExpress.XtraReports.Services; using System; using System.Web; namespace MyApp.Services { public class CustomTimeZoneProvider : ITimeZoneProvider { private readonly string timeZoneId; public CustomTimeZoneProvider() { timeZoneId = HttpContext.Current?.Request?.Cookies["LocalTimeZone"]?.Value; } public TimeZoneInfo GetCurrentTimeZone() { var tzId = timeZoneId ?? "UTC"; try { return TimeZoneInfo.FindSystemTimeZoneById(tzId); } catch { return TimeZoneInfo.Utc; } } } }At application startup, register the
CustomTimeZoneProviderservice:using DevExpress.XtraReports.Services; using DevExpress.XtraReports.Web; using MyApp.Services; namespace MyApp { public class Global_asax : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { // ... DevExpress.XtraReports.Web.WebDocumentViewer.DefaultWebDocumentViewerContainer.Register<ITimeZoneProvider, CustomTimeZoneProvider>(); // ... } } }In the view page, set the LocalTimeZone cookie to the client’s time zone:
@Html.DevExpress().WebDocumentViewer(settings => { settings.Name = "WebDocumentViewer1"; }).Bind("TestReport").GetHtml() <script> (function () { var tz = Intl.DateTimeFormat().resolvedOptions().timeZone; document.cookie = "LocalTimeZone=" + tz + "; path=/;"; })(); </script>
The sections that follow describe use cases for UtcToLocalTime() and LocalTimeToUtc() functions in reports.
Convert Date-Time Values in Data Fields
If your data source stores date-time values in UTC, use the UtcToLocalTime() function to display these values in the viewer’s local time zone.
Consider a label that displays data from the ShippedDate field. Field values are in the UTC time zone, and you want to display local times. Invoke the Expression Editor for the label’s Text property and specify the following custom expression:
UtcToLocalTime([ShippedDate])
Convert Date-Time Parameter Display Members
A date-time report parameter may obtain lookup values from a data source and these values may be stored in UTC. You can convert them to the viewer’s local time zone using the UtcToLocalTime() function.
Follow the steps below:
Create a calculated field that converts the UTC date-time values to the local time zone. For example, to convert the OrderDate field, create a calculated field with the following expression:
UtcToLocalTime([OrderDate])Bind the report parameter to the OrderDate field and set the Display Member property to the created calculated field.
The parameter’s lookup values will now be displayed in the viewer’s local time zone.
Convert Date-Time Parameter Values for Filtering
If end users use date-time report parameters to filter data, you may need to convert the parameter values from the viewer’s local time zone to UTC before applying the filter. To do this, use the LocalTimeToUtc() function in the filter expression.
For example, your report contains a dateRange parameter and the start and end parameter values are used to filter data by the OrderDate field. The date-time values are stored in the database in UTC, and users specify parameters in the viewer’s local time zone. To convert parameter values to UTC when applying the filter, specify the following filter expression for the report’s data source:
[OrderDate] Between(LocalTimeToUtc(?dateRange_Start), LocalTimeToUtc(?dateRange_End))