tags:

views:

28

answers:

2

Hi, I want to get the date and time of user's local system. How can I accomplish that in asp.net mvc? I am using mvc 1.0. Thanks.

+1  A: 

There's no way you can do that in server-side asp.net mvc code.

Either you prompt the user with DtropDown list of all timezones and let him pick it from the list and second option is to capture local system time in client-javascript and send it to server over an AJAX request or a post-back.

In fact there's one more way - You can capture the IP of the user's machine and find out the country to which that IP belongs to and pick the TimeZone of that country. You can use MaxMind GeoIP database which can help you do this very easily.

this. __curious_geek
Hi, I tried this approach too and to set the time with javascript and get it in server side, I used cookies and setTimeInterval method. This normally works fine but in case of ajax forms I dont know why it sends old time with cookies. Can you help with this? Thanks.
gaurav
@gaurav - Why are you sending the time in cookies? Why not just grab it once and then keep track of it for that user or session?
Justin
Got it, Thanks you all for giving your valuable time. :)
gaurav
+2  A: 

I think that you need to capture it via JavaScript and then send it back to the server (eg. using AJAX).

In the JavaScript: var d = new Date() will capture what you need.

http://www.w3schools.com/js/js_obj_date.asp

Justin
Hi Justin, I tried this approach too and to set the time with javascript and get it in server side, I used cookies and setTimeInterval method. This normally works fine but in case of ajax forms I dont know why it sends old time with cookies. Can you help with this? Thanks
gaurav