Jul 11, 2006 - 07:14 PM
Since your ASP.Net code runs on the server it will always use the time on the server. The usual approach is, to save the timezone with the user data. I.e let the user select his/her timezone and calculate the proper time on the server. Or get the time on the client side and send it to the server.
You get or set the CurrentCultureInfo or the CurrentUICulture on the running thread i.e. thread.CurrentCultureInfo. If you want to setthe Culture you create an object of type CulturInfo:
CulturInfo ci = CultureInfo.CreateSpecificCulture("en-US")
and then set the culture like this:
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
But as I said, this does not set the timezone, only the time format!!!
Hope this helped
Jul 11, 2006 - 07:49 PM
Do you know if it's possible in ASP.NET 2?
Anyway you get your points for explaining it, thanks.
Jul 11, 2006 - 08:01 PM
The thing most people just need a while to completely understand is, that ASP.NET code runs on the server and only on the server! If you want to do client side scriptiong you have to fall back to VBScript or Java Script! I guess if you need a timestamp for a user activity, your best bet is to send the time to the server.
var today = new Date()
offset = today.getTimezoneOffset()
You save this in a hidden field and you can send the offset from UTC in minutes to the server. Relatively easy I would say.
Jul 11, 2006 - 08:12 PM
The thing is that what I wanted was to save a timestamp on some of the information registered in the database and since the website has a fixed active timezone, but the server is in another, so it's not so much about displaying the time to the user as it is about recording it in the same timezone as all the rest of our activities.
And I was surprised that there weren't any elegant solutions.
Jul 11, 2006 - 08:49 PM
There is no more elegant solution since the timezone is a user credential and you will have to tell the server this information.
Hang on, I just found something else. There is a System.TimeZone class in .Net!!! It gives you the timezone based on the localization settings. But again, it will run on the server! Have a look at this webpage: http://www.csharphelp.com/archives/ar... And the TimeZone class is abstract so you can't create a timezone object and send it to the server. Not much help, is it?
Jul 11, 2006 - 08:57 PM
I'll have a look at the timezone class and see if that helps.
Thanks a lot.
Jul 11, 2006 - 09:10 PM
Look at the Qoumon time in each answer. You see that they obviously use the local server timezone. Where I live it is Wednesday, 10 am,12/07/2006. But my answers are marked with jul 11, 2006 23:57. And that's good. Just think about how confusing it would be if you write something on Tuesday, 23:00 and I'll reply with a time form Wednesday 10am and then you reply again with Tuesday etc. Confusing? You bet! So in some cases yo just want to use the timezone on the server. It is a generic setting. So think about the implication it wil lhave if yo change the time. Probably use two columns, one for server time and one for client time.
I think it was a good discussion. At least it made me think about this a bit more. And that's what I like about Quomon
Jul 11, 2006 - 08:02 PM