Link to home
Create AccountLog in
Avatar of sborchers
sborchers

asked on

Get ClientSide Date and Time

When my page is loaded, before the ASP (vb code) is run, I want to get the Client Date and Time from their PC.  The URL contains a 'timestamp', which I need to validate against the client side pc time.  I can get the date and time, but it runs after my Page_Load code.  Would like to read it before the Page_Load, so if the 'timestamp' is not valid, I don't need to run the rest of the code.
Avatar of hielo
hielo
Flag of Wallis and Futuna image

The browser does NOT send this information via headers when a request is submitted. So you CANNOT do this before the page loads. Your page must load first and then send the info to your application.
Avatar of sborchers
sborchers

ASKER

Ok, how could you see that the 'timestamp' doesn't match, and then send them to an expired page from the javascript?
Can you get the client side pc time from ASP (vb code)?
Can you explain what this "timestamp difference" is supposed to be used for "in the bigger picture". The reason I ask is that you will most likely need to do this in UTC due to difference in timezones.
a Delphi (windows) application is calling a webpage, which contains the local pc time in the url.  I want to compare that in the webpage to the local pc time, to see if it is valid.  If not valid, expire the page.
>>Can you get the client side pc time from ASP (vb code)?
NO. ASP runs on the server before the browser gets all the page's source code(HTML,Javascript,etc)

>>Ok, how could you see that the 'timestamp' doesn't match, and then send them to an expired page from the javascript?
What you can do is when the browser requests your page, you send the server's timestamp to the client. Then on the client, you can use javascript to get the client's timestamp and do the comparison on the client-side. If your criteria for redirection is met, then do so. See my code and comments below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
//get the server's date and time: Expecting a "proper" date string similar to
// Fri, 7 Mar 2008 
var serverStamp= Date.parse("<%=Now()%>");
 
//get the client's date and time
var clientStamp=new Date();
 
//compute the difference. Due to delay in download, the difference will likely
//be greater than zero most of the times, but choose an appropriate value that
//will suit your needs. I'm allowing at most 2000 milliseconds = 2 secs
if( 2000 < (serverStamp.valueOf() - clientStamp.valueOf()) )
{
	location.href="/exiredpage.asp";
}
</script>
</head>
<body>
 
</body>
</html>

Open in new window

BTW: The example is an ASP example, as you probably guessed based on this: <%=Now()%>"

Also, this line:
if( 2000 < (serverStamp.valueOf() - clientStamp.valueOf()) )

should have been :
if( 2000 < (clientStamp.valueOf() - serverStamp.valueOf() ) )
ok, thanks for the code sample.  looks like something i might be able to use.  can the serverstamp equal a textbox or session variable?

thanks again.
>> serverstamp equal a textbox or session variable
You can set serverstamp on the clientside upon load from a textbox or send it directly from the server either from the local system time or from a db or from a session variable. From what you were describing, it seems the correct way to go is to send the server's "real" time. To clarify, on my the sample code, this:
var serverStamp= Date.parse("<%=Now()%>");

is what your asp source code looks like, but when the user accesses that page, the browser will see something similar to this:
var serverStamp=Date.parse("Fri, 7 Mar 2008 23:00:00");

Just making sure you realize that what I was doing is creating the javascript dynamically. Not sure why you would want to use a session variable. Seems pointless to me.
yes it probably would be pointless if i didn't do what you have suggested.  except the url statement will contain the date and time (server) in that i need to compare against the clientside(javascript) date and time.

thanks!
any thoughts on replacing
var serverStamp= Date.parse("<%=Now()%>");
with a hiddenfield or textbox or any asp control?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks for all the help!  Your example worked just the way i needed it to.