473,486 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Client TimeZone

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?
Nov 18 '05 #1
9 15515
Javescript: Date.getTimezoneOffset()

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

"Allen Davis" <Al********@flex-n-gate.com> wrote in message
news:OB*************@TK2MSFTNGP09.phx.gbl...
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?

Nov 18 '05 #2
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.)

Nov 18 '05 #3
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]" <v-******@online.microsoft.com> wrote in message
news:yc**************@cpmsftngxa07.phx.gbl...
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.)

Nov 18 '05 #4
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.)

Nov 18 '05 #5
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.
Nov 18 '05 #6
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.)

Nov 18 '05 #7
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.
Nov 18 '05 #8
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/247referenc...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.)



Nov 18 '05 #9
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.)

Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3022
by: siliconmike | last post by:
I'm just lost here.. Is there a function to determine current time on the client's computer clock ? Links / Pointers? Mike
0
1336
by: Jason Kinkade | last post by:
I recently installed MySQL 4.0.18 on my slackware 9.1 machine (kernel 2.4.25) by compiling the source. It works fine, and all tests pass except one. The timezone test. Can someone tell me why...
8
3350
by: Zvonko | last post by:
Hi! I would like to get the clients date and time (his system time) and store it somewhere so I can use it in my code later. (insert it to database!). Any ideas Zvonko
7
3185
by: Jim Davis | last post by:
I'm (still) working on an ISO 8601 date parser. I want to convert at least the formats described here: http://www.w3.org/TR/NOTE-datetime Well.. I've got most of it working (via RegEx's) good...
1
4004
by: cnliou | last post by:
Hi! If I correctly understand v7.4 manual, value, say, '2003-11-26 12:00' in TIMESTAMP WITHOUT TIMEZONE column should output '2003-11-26 19:00' for "+08:00" timezone. The following test...
2
4735
by: David Garamond | last post by:
When a timestamp string input contains a timezone abbreviation (CDT, PST, etc), which timezone offset is used? The input date's or today date's? The result on my computer suggests the latter. #...
13
2822
by: Bruno Wolff III | last post by:
Recently there has been some discussion about attaching a timezone to a timestamp and some other discussion about including a 'day' part in the interval type. These two features impact each other,...
3
8302
by: asanford | last post by:
I want to create a web service that allows the caller to pass a DateTime to the web service (that is, create a web method such as void MyWebMethod(DateTime dt).) However, I want to be able to...
9
18492
by: praveen1983 | last post by:
i want to get client TimeZone name .for ex .if i am changing my timezone to CST it should change in my prgm thru javascript,and then it has to show the TimeZone name and current time also..
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.