"Zvonko" <zv****@velepromet.hr> wrote in message
news:df**********@ss405.t-com.hr...
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
As somebody else noted you want to grab the timezone information as well.
It's very easy to forget this (I happen to be knee deep in timezone code at
the moment) but without it your "client time" doesn't really mean anything.
For getting the code to your database you don't actually have to post a form
(although if you're already posting a form then this is a natural addition
to that). Any of the many techniques now falling under the "AJAX" umbrella
would work to get the data. One simple "old school" technique that works
pretty well across the board is adding the value as a query-string on an img
(or other resource) on the page.
The image tag would point to a server-side script that would evaluate the
query string, store (or do whatever with) the information and return the
image. Assuming that you're using ColdFusion as a server-side script (and
darn it why aren't you?) your img might look like this (abbreviated code -
the getTimeStamp() function would be a custom function that formatted your
information as you like):
<script>
document.write("<img src='GetClientTime.cfm?Time=" +
escape(getTimeStamp()) + "'>");
</script>
You could use a noscript tag to ensure that the img still displays when
script is disabled or use the old (and pretty cheesy) "single pixel GIF" gif
trick. If you want to get a little more complex (but really more "correct")
you could use a plain HTML img tag and change the source of it via script to
send the timestamp.
In any case that server-side GetClientTime.cfm would look for the "Time"
variable and do whatever you like with the value (probably store it). It
would then send the requested image down to the client. (In ColdFusion this
would be via the CFCONTENT tag but pretty much every server-side language
has some option to do this.)
I've a (warning!) very old article on doing this here:
<http://www.depressedpress.com/depressedpress/Content/Development/JavaScript/Articles/GIFAsPipe/Index.cfm>
The article actually covers techniques for doing bi-directional
communication (the returned image may contain a cookie) so the provided code
is somewhat more complex than you'd need.
Lastly you'll probably want to consider using an unambiguous datetime
format. The default Date.toString() method may work but this differs from
browser to browser.
Here is a rather verbose function to get a full iso8601 timestamp (local
time with timezone offset) from JavaScript :
function getTimeStamp() {
// Init DatePart vars
var Year,Month,Day,Hours,Minutes,Seconds,Milliseconds, TimeZoneInfo
// Get DateParts
Year = ("000" + CurDate.getFullYear()).slice(-4);
Month = ("0" + (CurDate.getMonth() + 1)).slice(-2);
Day = ("0" + CurDate.getDate()).slice(-2);
Hours = ("0" + CurDate.getHours()).slice(-2);
Minutes = ("0" + CurDate.getMinutes()).slice(-2);
Seconds = ("0" + CurDate.getSeconds()).slice(-2);
Milliseconds = ("00" + CurDate.getMilliseconds()).slice(-3);
// Get TimeZone Information
var TimeZoneOffset = CurDate.getTimezoneOffset();
TimeZoneInfo = (TimeZoneOffset >= 0 ? "-" : "+") + ("0" +
(Math.floor(Math.abs(TimeZoneOffset) / 60))).slice(-2) + ":" + ("00" +
(Math.abs(TimeZoneOffset) % 60)).slice(-2);
// Return the TimeStamp
return Year + "-" + Month + "-" + Day + "T" + Hours + ":" + Minutes +
":" + Seconds + "." + Milliseconds + TimeZoneInfo;
};
Sorry for being so long-winded... this just happened to coincide with some
work I'm doing. ;^)
Good luck!
Jim Davis