I am building an application in MVC3 and when a user comes into my site I want to know that user's timezone. I want to know how to do this in c# not in javaScript?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
As has been mentioned, you need your client to tell your ASP.Net server details about which timezone they're in.
Here's an example.
I have an Angular controller, which loads a list of records from my SQL Server database in JSON format. The problem is, the
DateTime
values in these records are in the UTC timezone, and I want to show the user the date/times in their local timezone.I determine the user's timezone (in minutes) using the JavaScript "
getTimezoneOffset()
" function, then append this value to the URL of the JSON service I'm trying to call:In my ASP.Net code, I extract the
timezoneOffset
parameter...... and pass it to my function which loads the records from the database.
It's a bit messy as I want to call my C#
FromUTCData
function on each record from the database, but LINQ to SQL can't combine raw SQL with C# functions.The solution is to read in the records first, then iterate through them, applying the timezone offset to the
DateTime
fields in each record.It works nicely though, and this code is really useful when writing a web service to display date/times to users in different parts of the world.
Right now, I'm writing this article at 11am Zurich time, but if you were reading it in Los Angeles, you'd see that I edited it at 2am (your local time). Using code like this, you can get your webpages to show date times that make sense to international users of your website.
Phew.
Hope this helps.