Convert UTC to local time with daylight saving on rdlc report

1.4k views Asked by At

I pick up data which includes some date columns. Date returned from database is in UTC format. I feed this data to report viewer and want to show the date in local format, considering daylight saving.

I can do that from server side but I want the conversion to be according to the client machine timezone.

Is there any way to do it ?

1

There are 1 answers

0
Iain On

Is your client a web page or a .NET app? If it is a .NET app you could do it like this. Firstly send your client timezone to your server like so:

string clientTimeZone = TimeZoneInfo.Local.ToSerializedString();

Then on your server:

var clientTimeZoneInfo = TimeZoneInfo.FromSerializedString(clientTimeZone);
var localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, clientTimeZoneInfo);

If this is a webapp use this javascript to send the offset to the server:

var timeNow = new Date();
var timezone = timeNow.getTimezoneOffset() / 60 * (-1);

Then on your server:

string clientTimeZoneOffset = "8";
var clientTimeZoneInfo = TimeZoneInfo.CreateCustomTimeZone("client", new TimeSpan(0, Int32.Parse(clientTimeZoneOffset), 0, 0),
            "client", "client");

var utcDateTime = DateTime.UtcNow;
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, clientTimeZoneInfo);