bp581
bp581

Reputation: 869

auto convert UTC date to local time zone

We are developing an ASP.net application which stores the date and time in UTC format. This application will be used globally. But each location will run separate instance of application and will have its own database.

Is it possible to auto convert the date and time when user is viewing the date time value without having to get the local time zone (Like EST,CST,PST ...) in C#.?

I can use as

DateTime convertTime = TimeZoneInfo.ConvertTimeFromUtc(
    DateTime.UtcNow, 
    TimeZoneInfo.Local);

but was wondering if I can make some application wide settings (in web.config ) or somewhere ... which will display all DateTime in local time zone format ?

Upvotes: 0

Views: 3173

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241890

In an ASP.Net application, TimeZoneInfo.Local refers to the time zone of the server - not the user. In general, it's a bad practice to rely on the server's time zone to be set to anything in particular.

The behavior you're asking about is already covered by the DateTime.Now property, which uses the local time zone, much in the way you showed in your code.

No, there's no way to use web.config settings or any other settings to globally change the local time zone. That's a per-machine setting, and is system-wide across all applications.

One approach you might consider is to pass UTC values down to the client, and use client-side JavaScript to convert to the user's local time. But if you have server-side conversions to do, then you must know the time zone of the user explicitly.

Upvotes: 1

Related Questions