DateTime.Now causing trouble when website uploaded on different server

My web site is in .net mvc. I have saved dates from client side using jQuery calendar into database MS SQL. Also I have saved dates using DateTime.Now. The problem is that the application is hosted on server which is in America, but my client is in Pakistan. They want time according to Pakistani time.

I tried to use TimeZoneInfo class but I am unable to use it as class library, or as DLL. How can I use a static sealed class and access its methods? Secondly I have used culture ur-PKR in web.config; it allows me to add dates in format dd/MM/yyyy which is working fine (according to PK style). Now after using culture style ur-PK, should I use Timezoneinfo class or not?

Jon Skeet
people
quotationmark

Firstly, you should separate DateTime formatting from time zones. They're completely separate problems.

For most scenarios (but not all), you should save values in your database in UTC, and convert them to the appropriate time zone when you need to display them to the client - that's also the point at which you should convert them to strings. (You should be storing the values in a DateTime field or something similar in your database - not varchar).

To convert from a UTC value to the Pakistan time zone, you just obtain the TimeZoneInfo with TimeZoneInfo.FindSystemTimeZoneById and then call TimeZoneInfo.ConvertTimeFromUtc. If you only need the same time zone everywhere, you can call FindSystemTimeZoneById just once and keep a reference to it.

// Ignore the "standard time" part - it really is both standard and daylight where
// necessary.
private static readonly TimeZoneInfo PakistanZone =
    TimeZoneInfo.FindSystemTimeZoneById("Pakistan Standard Time");

...

DateTime utc = ...; // DateTime.UtcNow, or a stored value
DateTime local = TimeZoneInfo.ConvertTimeFromUtc(utc, PakistanZone);

You might also want to consider using my Noda Time library, which makes it easier to keep track of which is local, which is an instant in time etc.

people

See more on this question at Stackoverflow