3

I have a C# MVC Web application hosted in a remote server. I don't know the exact location of that.

The users of that application are all from (UTC + 06.00) Dhaka. When a user inserts a new record, I want the inserted datetime from his local time e.i (UTC + 06.00) Dhaka.

How can I do it?

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • See webpage : https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23+culture+Dhaka+msdn & http://timtrott.co.uk/culture-codes/. Use : IFormatProvider provider = System.Globalization.CultureInfo.GetCultureInfo("bn-BD"); DateTime date = DateTime.Parse("6:00", provider); – jdweng Oct 23 '16 at 09:17
  • @jdweng - sorry, but time zone and culture are two very different things. Culture only affects date format, not time zone. – Matt Johnson-Pint Oct 25 '16 at 13:57

3 Answers3

7

The following code solves my problem-

DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo BdZone = TimeZoneInfo.FindSystemTimeZoneById("Bangladesh Standard Time");
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, BdZone);
s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • 1
    This is fine for conversion. However, you should probably not *store* local time in your database. In most cases, you only want to store UTC and do conversions for display. There are some exceptions, but as a general [best practice](http://stackoverflow.com/a/2532962/634824), UTC is how we store timestamps. – Matt Johnson-Pint Oct 25 '16 at 13:55
  • all time show same datetime even system time changed. can you post some more convenient code? – Mohammed Sabbir Jan 30 '23 at 04:04
1

You can follow below approach:

Step 1: You can store UTC time in database. If you are using SQL then you can use GETUTCDATE() to get UTC date.

Step 2

Please use Javascript to set a cookie, storing the browser timezone. (You can use scripts like jsTimeZoneDetect to get timezone name)

Step 3

Backend C# code:

  1. Pull timezone from cookie.

  2. Get the inserted utcTime from database and store in local variable(utcTime is the local variable name i used).

  3. Use below mentioned code to convert UTC time to local time.

    TimeZoneInfo tzoneinfo = TimeZoneInfo.FindSystemTimeZoneById("browser timezone name"); DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tzoneinfo);

Finally localtime is the end result. :)

Hope this will help you

Thank you

Deepak
  • 81
  • 5
  • Except, the result from jstz is not going to be usable by `TimeZoneInfo` on Windows. You'll have to convert from IANA to Windows first. – Matt Johnson-Pint Oct 25 '16 at 13:56
0

You should use global time zone of your ASP.NET application. Check this. This will work unless you use only C# datatime function. Once you have some complex code in SQL Server which will use GETTIME function. You will also rely on SQL Server timezone.

You should handle the internal datetime in UTC and convert the timezone only in UI.

Community
  • 1
  • 1
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • As the answer of the question you linked to states, there is no way to set a time zone globally in an asp.net application. Nor should you. – Matt Johnson-Pint Oct 25 '16 at 13:52