Client TimeZone

Discussion in 'ASP .Net' started by Allen Davis, Jan 9, 2004.

  1. Allen Davis

    Allen Davis Guest

    Is there no way in ASP.NET to retrieve the client's timezone (or even local
    time, I'd settle for that) and passing it to the server? There must be a way
    of determining the timezone from which the web browser is coming from...
    mustn't there?
     
    Allen Davis, Jan 9, 2004
    #1
    1. Advertising

  2. Allen Davis

    Guogang Guest

    Javescript: Date.getTimezoneOffset()

    You pass this value to a hidden field. Upon postback, you can read the
    value.

    "Allen Davis" <> wrote in message
    news:...
    > Is there no way in ASP.NET to retrieve the client's timezone (or even

    local
    > time, I'd settle for that) and passing it to the server? There must be a

    way
    > of determining the timezone from which the web browser is coming from...
    > mustn't there?
    >
    >
     
    Guogang, Jan 9, 2004
    #2
    1. Advertising

  3. Hi Allen,


    Thank you for using Microsoft newsgroup Service. Based on your description,
    you want to get the UTC offset of the client side in ASP.NET serverside or
    just transfer such info to serverside? Is my understanding correct?

    As for this question, I think you Guogang's suggestion is quite good.
    Though in dotnet framework, there isn't direct interface for you to get the
    client side's UTC offset, you can do it pretty easily via some clientside
    script. Just as the Gogang said that use the javascript's code
    "Date.getTimezoneOffset()" and transfer the result to serverside by a
    hidden input area. For example
    use such page elements:

    <td>
    <input type="button" value="getclientutc" onclick="GetClientUTC()">
    <input type="hidden" id="hdClientUTC" runat="server">
    </td>

    then add the javascript function:
    <script language="javascript">

    function GetClientUTC()
    {
    var now = new Date()
    var offset = now.getTimezoneOffset();
    document.Form1.hdClientUTC.value = offset
    }
    </script>

    Please try out the suggestion. If you have any questions, please feel free
    to let me know.



    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 10, 2004
    #3
  4. Allen Davis

    Allen Davis Guest

    Steven,

    Thanks for the function. It works just fine. I've placed it in the <HEAD>
    section of my ASPX page and can successfully pass the value of the hidden
    variable to the server if I use the button. But I'm Java clueless so bear
    with me and answer another question if you're willing, please.

    How can I execute that JavaScript function when the page loads so the
    variable is immediately available to my server-side code? I tried a few
    things including the <body onload> method but haven't had much luck.

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Allen,
    >
    >
    > Thank you for using Microsoft newsgroup Service. Based on your

    description,
    > you want to get the UTC offset of the client side in ASP.NET serverside or
    > just transfer such info to serverside? Is my understanding correct?
    >
    > As for this question, I think you Guogang's suggestion is quite good.
    > Though in dotnet framework, there isn't direct interface for you to get

    the
    > client side's UTC offset, you can do it pretty easily via some clientside
    > script. Just as the Gogang said that use the javascript's code
    > "Date.getTimezoneOffset()" and transfer the result to serverside by a
    > hidden input area. For example
    > use such page elements:
    >
    > <td>
    > <input type="button" value="getclientutc" onclick="GetClientUTC()">
    > <input type="hidden" id="hdClientUTC" runat="server">
    > </td>
    >
    > then add the javascript function:
    > <script language="javascript">
    >
    > function GetClientUTC()
    > {
    > var now = new Date()
    > var offset = now.getTimezoneOffset();
    > document.Form1.hdClientUTC.value = offset
    > }
    > </script>
    >
    > Please try out the suggestion. If you have any questions, please feel free
    > to let me know.
    >
    >
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
     
    Allen Davis, Jan 12, 2004
    #4
  5. Hi Allen,


    Thanks for your response. I'm very glad that my suggestion has helped you.
    As for the problem you mentioned in the reply, my understanding is:
    You want to get the client's UTC offset information at the serverside in
    the first time the page is loaded( in the Page_Load event)? Please feel
    free to correct me if I misunderstand your problem.


    As for the <body onload=".." ..> event you set in the page, it is a
    clientside event which will be fired when the page is requested and
    returned back to the clientside's browser. And that is after the first time
    the page is loaded on the serverside(Page_Load event). So when the page is
    first time loaded on the serverside(first time Page_Load is fired), no
    client offset info is avaliable. Only after the page is post back to
    serverside again(this time the client utc offset info has been set into
    those hidden fields via the clientside javascript).

    If you think it is necessary to get this information in advance on the
    serverside. I've a suggestion that you try check the Page_Load event, if it
    is first called(not PostBack), then you set a period of client script using
    the Page.RegisterStartupScript" to let the page be posted back right after
    get the client utc offset info. How do you think of this?

    Please check the above suggestion. If you have anything unclear on them,
    please feel free to let me know.



    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 13, 2004
    #5
  6. Steven

    Another very good suggestion for the JavaScript neophyte. So here's what I did. In my code-behind page (I haven't added the comment tags to the javascript yet, they were tripping me up)..

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
    Dim scriptGetClientUTCOffset As String = "<script language=javascript>
    scriptGetClientUTCOffset &= "var now = new Date();
    scriptGetClientUTCOffset &= "var offset = now.getTimezoneOffset();
    scriptGetClientUTCOffset &= "document.Form1.hidClientUTCOffset.value = offset;
    scriptGetClientUTCOffset &= "document.Form1.submit()
    scriptGetClientUTCOffset &= "</script>

    If Not Page.IsPostBack The
    If (Not Me.IsStartupScriptRegistered("Startup")) The
    Me.RegisterStartupScript("Startup", scriptGetClientUTCOffset
    End I
    Calendar.SelectedDate = Toda
    End I
    Label1.Text = hidClientUTCOffset.Valu
    DataList1.DataBind(
    End Su

    I use that together with <input type="hidden" id="hidClientUTCOffset" runat="server"> in my ASPX page and, as soon as the page is opened, Label1's text is set to the client's UTC offset in minutes (I'm assuming as an integer)

    So this has certainly solved my dilemna... now if I can just figure out how to take that value and use it in conjunction with the TimeZone object to produce some effective results. Something besides a little 300 or 360 in the upper-left corner of my page. :

    Thanks again for your direction on this

    ----- Steven Cheng[MSFT] wrote: ----

    Hi Allen


    Thanks for your response. I'm very glad that my suggestion has helped you.
    As for the problem you mentioned in the reply, my understanding is
    You want to get the client's UTC offset information at the serverside in
    the first time the page is loaded( in the Page_Load event)? Please feel
    free to correct me if I misunderstand your problem


    As for the <body onload=".." ..> event you set in the page, it is a
    clientside event which will be fired when the page is requested and
    returned back to the clientside's browser. And that is after the first time
    the page is loaded on the serverside(Page_Load event). So when the page is
    first time loaded on the serverside(first time Page_Load is fired), no
    client offset info is avaliable. Only after the page is post back to
    serverside again(this time the client utc offset info has been set into
    those hidden fields via the clientside javascript).

    If you think it is necessary to get this information in advance on the
    serverside. I've a suggestion that you try check the Page_Load event, if it
    is first called(not PostBack), then you set a period of client script using
    the Page.RegisterStartupScript" to let the page be posted back right after
    get the client utc offset info. How do you think of this

    Please check the above suggestion. If you have anything unclear on them,
    please feel free to let me know



    Steven Chen
    Microsoft Online Suppor

    Get Secure! www.microsoft.com/securit
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.
     
    =?Utf-8?B?QWxsZW4gRGF2aXM=?=, Jan 13, 2004
    #6
  7. Hi Allen,

    Thanks for your response. I'm very glad that we've resolved this issue.
    Have a good day!


    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 14, 2004
    #7
  8. As a follow up to my last post, here's the function I use to turn that client offset integer into something useful.

    Function LocalTime(ByVal dtm As DateTime)
    Dim dtmDBUTC As DateTime = dtm.ToUniversalTime
    Dim dtmClient As DateTime = dtmDBUTC.AddMinutes(-hidClientUTCOffset.Value)
    Return dtmClient
    End Function

    I still have no leads on how to correctly reflect the English standard name for the client's time zone though. That seems to be a much more difficult challenge.
     
    =?Utf-8?B?QWxsZW4gRGF2aXM=?=, Jan 21, 2004
    #8
  9. Hi Allen,


    Thanks for your followup. As for the problem how to using the Timezone, I
    think you can make full use of the javascript functions to do much more
    operations onclient side so as not to put all the calculations on the
    serverside. The javascript object "Date" has many useful functions, for
    example:
    ---------------------------------------------------
    Methods Explanation NN IE ECMA
    Date() Returns a Date object 2.0 3.0 1.0
    getDate() Returns the date of a Date object (from 1-31) 2.0 3.0 1.0
    getDay() Returns the day of a Date object (from 0-6. 0=Sunday, 1=Monday,
    etc.) 2.0 3.0 1.0
    getMonth() Returns the month of a Date object (from 0-11. 0=January,
    1=February, etc.) 2.0 3.0 1.0
    getFullYear() Returns the year of a Date object (four digits) 4.0 4.0 1.0
    getYear() Returns the year of a Date object (from 0-99). Use getFullYear
    instead !! 2.0 3.0 1.0
    getHours() Returns the hour of a Date object (from 0-23) 2.0 3.0 1.0
    getMinutes() Returns the minute of a Date object (from 0-59) 2.0 3.0 1.0
    getSeconds() Returns the second of a Date object (from 0-59) 2.0 3.0 1.0
    getMilliseconds() Returns the millisecond of a Date object (from 0-999) 4.0
    4.0 1.0
    getTime() Returns the number of milliseconds since midnight 1/1-1970 2.0
    3.0 1.0
    getTimezoneOffset() Returns the time difference between the user's computer
    and GMT 2.0 3.0 1.0
    getUTCDate() Returns the date of a Date object in universal (UTC) time 4.0
    4.0 1.0
    getUTCDay() Returns the day of a Date object in universal time 4.0 4.0 1.0
    getUTCMonth() Returns the month of a Date object in universal time 4.0 4.0
    1.0
    getUTCFullYear() Returns the four-digit year of a Date object in universal
    time 4.0 4.0 1.0
    getUTCHours() Returns the hour of a Date object in universal time 4.0 4.0
    1.0
    getUTCMinutes() Returns the minutes of a Date object in universal time 4.0
    4.0 1.0
    getUTCSeconds() Returns the seconds of a Date object in universal time 4.0
    4.0 1.0
    getUTCMilliseconds() Returns the milliseconds of a Date object in universal
    time 4.0 4.0 1.0
    parse() Returns a string date value that holds the number of milliseconds
    since January 01 1970 00:00:00 2.0 3.0 1.0
    setDate() Sets the date of the month in the Date object (from 1-31) 2.0 3.0
    1.0
    setFullYear() Sets the year in the Date object (four digits) 4.0 4.0 1.0
    setHours() Sets the hour in the Date object (from 0-23) 2.0 3.0 1.0
    setMilliseconds() Sets the millisecond in the Date object (from 0-999) 4.0
    4.0 1.0
    setMinutes() Set the minute in the Date object (from 0-59) 2.0 3.0 1.0
    setMonth() Sets the month in the Date object (from 0-11. 0=January,
    1=February) 2.0 3.0 1.0
    setSeconds() Sets the second in the Date object (from 0-59) 2.0 3.0 1.0
    setTime() Sets the milliseconds after 1/1-1970 2.0 3.0 1.0
    setYear() Sets the year in the Date object (00-99) 2.0 3.0 1.0
    setUTCDate() Sets the date in the Date object, in universal time (from
    1-31) 4.0 4.0 1.0
    setUTCDay() Sets the day in the Date object, in universal time (from 0-6.
    Sunday=0, Monday=1, etc.) 4.0 4.0 1.0
    setUTCMonth() Sets the month in the Date object, in universal time (from
    0-11. 0=January, 1=February) 4.0 4.0 1.0
    setUTCFullYear() Sets the year in the Date object, in universal time (four
    digits) 4.0 4.0 1.0
    setUTCHour() Sets the hour in the Date object, in universal time (from
    0-23) 4.0 4.0 1.0
    setUTCMinutes() Sets the minutes in the Date object, in universal time
    (from 0-59) 4.0 4.0 1.0
    setUTCSeconds() Sets the seconds in the Date object, in universal time
    (from 0-59) 4.0 4.0 1.0
    setUTCMilliseconds() Sets the milliseconds in the Date object, in universal
    time (from 0-999) 4.0 4.0 1.0
    toGMTString() Converts the Date object to a string, set to GMT time zone
    2.0 3.0 1.0
    toLocaleString() Converts the Date object to a string, set to the current
    time zone 2.0 3.0 1.0
    toString() Converts the Date object to a string 2.0 4.0 1.0
    ----------------------------------------------------------------------------
    -------------------

    For more detailed info on this javascript class, you may view the following
    reference:
    http://www.w3schools.com/js/js_datetime.asp

    In addition, here is some tech articles on how to deal with the UTC
    time(both clienside or serverside)
    #Javascript Date and Time 5 : Date and Time Elsewhere
    http://www.merlyn.demon.co.uk/js-date5.htm

    #Date/Time display in local time
    http://www.dotnet247.com/247reference/msgs/22/110400.aspx

    Hope they are helpful.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 23, 2004
    #9
  10. Hi Allen,

    As for the problem "get the Timezone via a specified offset value", I've
    done some further researchs and asked some other specialists for infos. It
    seems that via Win32 and .Net, the only way to get a Timezone name, given
    only an offset, would be to create a lookup table. So do you think it
    proper to use a manually created lookup table to workaround this prolbem?

    Please feel free to post here if you have any further questions.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jan 29, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. marcie
    Replies:
    0
    Views:
    611
    marcie
    Apr 28, 2004
  2. asaf
    Replies:
    3
    Views:
    6,228
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 11, 2006
  3. Adam Lipscombe
    Replies:
    2
    Views:
    992
  4. axrock
    Replies:
    8
    Views:
    994
    Roedy Green
    Mar 25, 2009
  5. Ami
    Replies:
    5
    Views:
    305
Loading...

Share This Page