How to store date and time consistent with internationalization

I have a project that will have a dropdown at the top to select countries. For example, USA, India and Hong Kong. Several features will be enabled or disabled depending on the country selected.

In a project I have a default base country like India. Therefore whenever any user comes. I want to keep the login date and time according to Indian Standard Time, regardless of the user from Hong Kong, USA or India. My web server is located in the USA.

Is it possible?

0


a source to share


3 answers


What you want to do is store any datetime value as UTC . This means storing the value as time zone independent.

You can set some sort of insert trigger on your MS-SQL server (I'm assuming you are using MS-SQL because of the asp.net part). There you define UTC time like this.

`-- MS SQL (use instead of GETDATE())
DECLARE @currentTime datetime
SET @currentTime = GETUTCDATE()`

      

If you want to do it in code, just use UtcTimeNow to access UTC time.



    // C# (use instead of DateTime.Now)
DateTime currentTime = DateTime.UtcNow;

      

If you want, you can always convert UTC time to any local time. Using the TimeZoneInfo class .

This way you have a suitable time in your database (UTC) and you can convert it to the appropriate time for the user.

The most important part about time and time zones is storing it as UTC. Thus, it can be easily transported and compared.

+1


a source


If you are storing your time in unixtime, you can have a field in the user table called offset. Where is this value, how much the system should compensate for the time.

Then you just get that variable and store it as a session value or something similar. And use that to offset the date and time on the system.



For instance. Hong Kong is +8 hours from GMT. Currently GMT Unixtime: 1242293400 (9:30). 8 hours - 28800 seconds. Then you will save this value to your server. So the time will be 1242322200 (17:30 on the same day)

+1


a source


IF you are using SQL Server 2008, take a look at the new datetime datatypes which will also allow you to store the offset / timezone.

0


a source







All Articles