Server Time Revisited

  • Thread starter Thread starter sandman
  • Start date Start date
S

sandman

I've been testing my web app on another workstation to simulate using
the server time. The test pc's time is an hour behind the server time
and when the user processes a request, the server time is picked up.
The problem is that I've got a "clock" on the web page that updates the
"current time" every second since the app is time critical. The clock
is a jscript routine. It seems to be picking up the local time. I'm
just instantiating a new Date() object (when the page loads), which
according to the docs is making a System.DateTime call. Is there a way
to seed it or get it to pick up the server time instead?

margaret
 
Hi Margaret,


These are the constructor available for Date():
new Date()new Date(milliseconds)new Date(dateString)new Date(yr_num, mo_num,
day_num [, hr_num, min_num, sec_num, ms_num])Parameters
milliseconds Integer value representing the number of milliseconds since 1
January 1970 00:00:00.

dateString String value representing a date. The string should be in a
format recognized by the Date.parse method.

yr_num, mo_num,day_num Integer values representing part of a date. As an
integer value, the month is represented by 0 to 11 with 0=January and
11=December.




as you can see you can set it as you need it, now the tricky part would be
how to transfer the server date to the client, the code below will do it, be
aware I did not test it just write it from OE :)
<%
// Server side code
void WriteDateToClient(){
Literal lit = new Literal;
lit.Text = "<script>\n " +
var yr_num=" + DateTime.Now.Year.ToString() + ";\n" +
var mo_num=" + (DateTime.Now.Month-1).toString() + ";\n"
+ // .Net DateTime.Month is from 1 to 12
var day_num=" + DateTime.Now.Day.ToString() + ";\n" +
//.. do the same with the Time part
this.Controls.Add( lit ); // this include the literal in the page control
list
}

%>

//in the client side, you know you have define the variables you are going
to use
<script>
var ServerDate = new Date( yr_num, mo_num, .... );
</script>

Now you have the server time in the client :)
all you have to remember is always call the method in the server side, you
can get this by calling it in the Load handler.


Hope this help,


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

----- Original Message -----
From: "sandman" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Tuesday, October 21, 2003 12:44 PM
Subject: Server Time Revisited
 
I looked at my code again. I'm getting the server time over there but
the JScript that I'm running at the client side is getting a new Date()
every minute in order to show a "live" clock. That's what's stomping on
the server date. I'm hesitant to ask for the server for the date each
time because of the volume of the requests. When I tried using an
alternative constructor, it never updates because the parameters never
change. So whatever I use to construct the date also has to update
every minute. But there's no such thing as global variables in a
webpage right?

Ignacio, the code for the literal's Text property totally confused me.
It looks like you're declaring variables in the middle of it.

margaret
 
Margaret,

You can work around this by storing the offset between the server time and
the client time. When you need to update the displayed time, just adjust
the current client time by the stored offset.

HTH,
Nicole
 
Hi Margaret,

That's exactly what I'm doing :)
I'm generating Javascript code form the server side, I'm generating a <script> section that will be executed in the client, inside this section I declare the variables I need to init a new Date instance directly in the client but with the server time.


Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

I looked at my code again. I'm getting the server time over there but
the JScript that I'm running at the client side is getting a new Date()
every minute in order to show a "live" clock. That's what's stomping on
the server date. I'm hesitant to ask for the server for the date each
time because of the volume of the requests. When I tried using an
alternative constructor, it never updates because the parameters never
change. So whatever I use to construct the date also has to update
every minute. But there's no such thing as global variables in a
webpage right?

Ignacio, the code for the literal's Text property totally confused me.
It looks like you're declaring variables in the middle of it.

margaret
 
Back
Top