SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setting server's timezone in asp?

    Hi,

    I host my web application with a USA company, which using USA timezone.
    My audience is from Malaysia which is in different timezone.

    How to set the server timezone in asp?

    I wish to get a solution which, after I set the timezone, response.write Now() can return the Malaysia local time.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's tough, and with vbscript pretty much impossible. JScript to the rescue

    Code:
    <script runat="server" language="jscript">
    //you'll have to switch this variable for savings time or have some calculation that does it automatically
    var MALASIA_OFFSET = 480; // Malasian time is 480 minutes before UTC time ???? I think ????  
    
    function NowMalasia() {
    	var d = new Date();  // current server date/time
    	var offset = d.getTimezoneOffset(); // difference in minutes between server time and UTC
    	d.setMinutes(d.getMinutes() + offset + MALASIA_OFFSET);
    	return d;
    }
    </script>
    <&#37;
    	Response.Write NowMalasia()
    %>

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    However by using the method mention above, I have to REPLACE ALL the built-in time function with my custom functions.

    Is that possible to change the system setting, so that Now() will return Malaysia local time? I know changing it in my computer is possible, but my web application is shared-hosted with USA company. I wonder if they can assign different timezone setting for each of their customer.

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    @Jim -> not heard of the DateAdd function?

    @Janet -> you can use @codepage directives that set certain parameters, but I'm not sure you can actually make the server date/time different.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah,

    After doing some experiment, i think the jimfraser solution works!

    The key is the value of getTimezoneOffset() is not constant it will be adjusted according to Summer Time (which is basically depend on the Windows)

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DateAdd, sure, but you can't get the timezone offset using vbscript, that I know of.

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, i jst realized that the method we use so far, is assume the target timezone has a constant offset.

    for example, var MALAYSIA_OFFSET = 480;

    However, if the server is in USA, and the target audience is from UK (British Summer Time), both timezone have a non-constant offset, how to deal with that?

    for the server, it's easy. the javascript getTimezoneOffset() can help. But how to get the target timezoneOffset when it's not fixed (may advance one hour or fall back one hour)?

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Hull
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is this any use: http://support.microsoft.com/kb/q229690/ hopefully then no matter were the origin of your users they will see the correct time and if used date in the prefered format as I know personally from developing a major thing is setting the date from the standard American format to our UK version.

  9. #9
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the format isn't my main concern.

    dlw, since you are from UK. How do u deal with your time? your uk standard time is GMT +0000, and during summer, you use BST, which advance one hour.
    So how do u deal with this on your webpage? you display the standard time or BST time during summer time?

  10. #10
    SitePoint Member El Fuego's Avatar
    Join Date
    Dec 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But most servers automatically adjust for Daylight Savings.
    --------------
    El Fuego
    --------------

  11. #11
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could either keep track of start/end of DST for your target region and do conversions on the server (lots of work and keeping track of dates/times etc every year it changes) OR...

    - display the time using javascript relying on the client's clock (simple and very inelegant)
    - at the beginning of the user's session, run a javascript that determines the user's timezoneOffset and send it to the server, to be stored in a session variable. There you have the correct amount to adjust. Example:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function getTZ() {
    	var d = new Date();
    	document.forms[0].elements["tzOffset"].value = d.getTimezoneOffset();
    	return true;
    }
    </script>
    </head>
    <body>
    <form method="post" action="setTZ.asp" onsubmit="return getTZ();">
    	<input type="hidden" name="tzOffset" value="0" />
    	<input type="submit" value="Continue" />
    </form>
    </body>
    </html>
    on the server side...
    setTZ.asp
    Code:
    Session("userTZOffset") = CLng(Request.Form("tzOffset"))
    Then instead of using the variable MALASIA_OFFSET in the function I posted before, use Session("userTZOffset")

    There you have a global solution that caters to users in any country, as long as they have the timezone set correctly on their computers.

  12. #12
    SitePoint Member El Fuego's Avatar
    Join Date
    Dec 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quite elegant ... except for those anal people that disable JavaScript!

    --------------
    El Fuego
    --------------

  13. #13
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    basically I want to insert the local date/time into database, and retrieve it using local time.

    the idea of getting the timezoneoffset is workable if we just want to display the data to user. However if we want to insert it into database, the time data depend on the user's system is not reliable (imagine if they set the timezone wrongly on their computer)

  14. #14
    SitePoint Member El Fuego's Avatar
    Join Date
    Dec 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dates/Times in the database should ALWAYS be based on the SERVER date/time setting. That way you can insure consistency amongst all entries, regardless of the geographic location of each user.

    Use the time zone and offset merely for display to the user (such as like SitePoint here does).
    --------------
    El Fuego
    --------------

  15. #15
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with el fuego

  16. #16
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok

    It seems the best way to go with:
    when storing data, use server date/time setting.
    when retrieving data, use visitor's date/time setting and retrieve the offset, and manipulate it with the output time data.

    The only drawback is if the user has incorrect timezone setting on his/her computer, the user might get unexpected result.

  17. #17
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Will this be a "registered users" utility? (e.g. like this forum is). If so then get them to define their timezone when registering and store that also in the DB. Then when you display anything to the user simply take account of the stored timezone offset.
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •