Get Exact Time Via Web Site

Bob Schneider
Bob Schneider used Ask the Experts™
on
I need to capture the exact time and write it to a sql server db via a classic asp web page.  I have a page that does that now...sort of.  It uses the Now() asp function.  The problem is that, if the internet happens to be slow where we are, by the time the command goes to the server, the time may not be as precise as we need.  Is there a way to capture that time client side (using javascript for instance) and then write it to the server, all with one button submit?

Thanks in advance for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Interesting requirement...

If you would have 10 clients connected then each of them will send different time to the server? OK, why not. BUT forget about accuracy.

You may just do what you've proposed extended by the current time reading from SQL Server.

In more details - read the GETDATE() function result from SQL Server query and return this time together with Now() function result back to the server. The server must record the 2 values + GETDATE() again. The difference between the 2 GETDATE() results will show the network speed, the Now will show the local time. Just remember the down and up network speed is not symmetric obviously...

Also read this article which should show how to make the whole stuff more accurate: https://www.codeproject.com/Articles/790220/Accurate-Time-in-JavaScript
leakim971Multitechnician
Top Expert 2014

Commented:
1) evaluate the time lag(shift, gap,interval) between the client and your server. you need to do this test more than one time, not to update the difference but to do a constant average near to the real one.
2) now using this average updated at a good frequency, you just need to get the time on your server and add this shit to your server error.

what you need is to update this time lag, the gap between the client and server, you don't rely on client time really
Bob SchneiderCo-Owner

Author

Commented:
So there is no client-side time function that I could access on a submit that I could then save in a sql server db using a classic asp page?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

leakim971Multitechnician
Top Expert 2014

Commented:
<input type="hidden" id="time" name="time">
</form>
<script>
    document.getElementsByTagName("form")[0].onsubmit = function() {
          document.getElementById("time").value = new Date().getTime(); // number of millisecond from 01/01/1970
    }
</script>
</body>
</html>

Open in new window

Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
Adding to what leakim provided, you can get the time zone data in minute from utc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset.

But really, as long as you capture both the start and end time using javascript, it does not matter what the time zone is, you are just capturing the difference.

For capturing time server side, I typically just create a datetime field in sql server and just set the default to getdate(). Then no need to input that using now() in asp.  

The only issue you will have with doing this via javascript is javascript can be manipulated by the user. If you were using this for timing a racer, it may be more accurate to use a timing box of some sort that can send data to your server rather than relying on the browser. Or create a client timing/stopwatch app that can send data to the server.  For general purposes though, the javascript idea sounds easy. Just make sure to store the "start" and "stop" on the client. Once stop is clicked, then calculate the difference in time and send that to your server via an ajax request or directly.
Bob SchneiderCo-Owner

Author

Commented:
Thanks everyone.  We do use a timing system but, since I can't start it remotely when the start is some distance away, I need a way to record the exact time that the race started.  I have a manual process and it works well, but it relies on the timing box and the computer being in sync.  I also have a way to sync my PC time to Network Time, which is what the timing box automatically syncs to.  This all works well for me but some of the folks that do timing for me are not as "diligent" and timing help is not easy to find.

Right now I have a nice little asp page that does exactly what I want to do in that it uses the Now() function to record the exact time that the race started and logs it into my db.  The  ONLY problem is the potential lag if we are timing an event in a remote area where the internet is slow.  My thought was to grab the current time using a client side process.  Then, sending that to my db would not time sensitive because I grabbed the time before I sent it to a remote db.
Developer & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
If you are the only one using the page, that makes sense to at least test.

A quick search and somebody has a very simple stop watch https://gist.github.com/electricg/4372563#file-stopwatch-js
https://jsbin.com/keliyuluse/edit?html,js,output
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Stopwatch</title>
</head>
<body onload="show();">
	<div>Time: <span id="time"></span></div>
	<input type="button" value="start" onclick="start();">
	<input type="button" value="stop" onclick="stop();">
	<input type="button" value="reset" onclick="reset()">
</body>
</html>

Open in new window

//	Simple example of using private variables
//
//	To start the stopwatch:
//		obj.start();
//
//	To get the duration in milliseconds without pausing / resuming:
//		var	x = obj.time();
//
//	To pause the stopwatch:
//		var	x = obj.stop();	// Result is duration in milliseconds
//
//	To resume a paused stopwatch
//		var	x = obj.start();	// Result is duration in milliseconds
//
//	To reset a paused stopwatch
//		obj.stop();
//
var	clsStopwatch = function() {
		// Private vars
		var	startAt	= 0;	// Time of last start / resume. (0 if not running)
		var	lapTime	= 0;	// Time on the clock when last stopped in milliseconds

		var	now	= function() {
				return (new Date()).getTime(); 
			}; 
 
		// Public methods
		// Start or resume
		this.start = function() {
				startAt	= startAt ? startAt : now();
			};

		// Stop or pause
		this.stop = function() {
				// If running, update elapsed time otherwise keep it
				lapTime	= startAt ? lapTime + now() - startAt : lapTime;
				startAt	= 0; // Paused
			};

		// Reset
		this.reset = function() {
				lapTime = startAt = 0;
			};

		// Duration
		this.time = function() {
				return lapTime + (startAt ? now() - startAt : 0); 
			};
	};

var x = new clsStopwatch();
var $time;
var clocktimer;

function pad(num, size) {
	var s = "0000" + num;
	return s.substr(s.length - size);
}

function formatTime(time) {
	var h = m = s = ms = 0;
	var newTime = '';

	h = Math.floor( time / (60 * 60 * 1000) );
	time = time % (60 * 60 * 1000);
	m = Math.floor( time / (60 * 1000) );
	time = time % (60 * 1000);
	s = Math.floor( time / 1000 );
	ms = time % 1000;

	newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3);
	return newTime;
}

function show() {
	$time = document.getElementById('time');
	update();
}

function update() {
	$time.innerHTML = formatTime(x.time());
}

function start() {
	clocktimer = setInterval("update()", 1);
	x.start();
}

function stop() {
	x.stop();
	clearInterval(clocktimer);
}

function reset() {
	stop();
	x.reset();
	update();
}

Open in new window

What you will want to do is add a function perhaps using ajax to hit the database with the final time that posts to your database. Probably another button "Post to database".   I think I would hide the reset button so it does not get clicked by accident. I would also record each key press to the database too.
Bob SchneiderCo-Owner

Author

Commented:
Thank you everyone!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial