Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / August 2005



Tip: Looking for answers? Try searching our database.

Storing client date and time

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Zvonko - 30 Aug 2005 13:56 GMT
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
McKirahan - 30 Aug 2005 14:36 GMT
> Hi!
>
[quoted text clipped - 4 lines]
>
> Zvonko

One possible approach which requires that JavaScript be enabled:

<html>
<head>
<title>page1.htm</title>
<body>
<form action="page2.htm" method="get" name="form1">
<input type="hidden" name="When" size="10" value="yyyymmddhhnnss">
</form>
<script type="text/javascript">
var when = new Date();
var form = document.form1;
   form.When.value  = when.getFullYear();;
   form.When.value += (100+(when.getMonth()+1)+"").substr(1);
   form.When.value += (100+(when.getDate())+"").substr(1);
   form.When.value += (100+(when.getHours())+"").substr(1);
   form.When.value += (100+(when.getMinutes())+"").substr(1);
   form.When.value += (100+(when.getSeconds())+"").substr(1);
alert(form.When.value + "\n" + "yyyymmddhhnnss");
   form.submit();
</script>
</body>
</html>
Dr John Stockton - 31 Aug 2005 00:26 GMT
JRS:  In article <xLmdnRZ82OFr_YneRVn-tw@comcast.com>, dated Tue, 30 Aug
2005 08:36:13, seen in news:comp.lang.javascript, McKirahan
<News@McKirahan.com> posted :

>> 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!).

You should consider storing also the client offset from GMT.  However,
if the database is not on the client computer, you should consider using
the server date and time, for reliability.

>var when = new Date();
>var form = document.form1;
[quoted text clipped - 6 lines]
> alert(form.When.value + "\n" + "yyyymmddhhnnss");
>    form.submit();

You have been told that an efficient way of making a small integer into
a two-digit integer in VBScript is to add 100 and use the end of the
string.  Do not assume that the same holds in javascript; it is
relatively inefficient there.

Do not repeat code; Leading Zero should be a function, unless you can
show that coding it in-line is significantly quicker in context.

It cannot be efficient to evaluate the location of form.When.value
several times; remove all  form.When.value +=  and change the preceding
semicolons to pluses.

There is no need, however, to do any string manipulation; arithmetic
will be quicker, as has been indicated in the MS VBScript newsgroup.

       with (when) Ans =
         ((((getFullYear()*100+getMonth()+1)*100+getDate())
         *100+getHours())*100+getMinutes())*100+getSeconds()
       form.Wnen.value = Ans
       alert(Ans + "\n" + "yyyymmddhhmmss")

In the microsoft newsgroups, you have consistently been unduly hasty in
responding and have wasted people's time by presenting inefficient code.
There is no need for you to do the same here.  You should devote more of
your time to learning from the responses to the amateurish articles that
you post; it will be better for your reputation when you grow up.

Signature

© John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 ©
<URL:http://www.jibbering.com/faq/>  JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

McKirahan - 31 Aug 2005 02:10 GMT
[snip]

> You have been told ...

[snip]

John, I prefaced my solution with:  "One possible approach ...".

Instead of addressing me why don't you just address the OP?

All you had to say is "Here's a preferred approach.".

> In the microsoft newsgroups, you have consistently been unduly hasty in
> responding and have wasted people's time by presenting inefficient code.

[snip]

I'm faulted for offering "a" solution when it is requested!

I guess the world should wait for you to answer all of their questions...

> --
>  © John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk

Perhaps you should spend a little more time working on your social skills
...
Randy Webb - 31 Aug 2005 06:54 GMT
McKirahan said the following on 8/30/2005 9:10 PM:

> [snip]
>
[quoted text clipped - 7 lines]
>
> All you had to say is "Here's a preferred approach.".

But, had he done that, it would not have fed his ego, made him feel
important and would not have allowed him to be pedantic enough. He is a
hypocritical idiot about some things.

>>In the microsoft newsgroups, you have consistently been unduly hasty in
>>responding and have wasted people's time by presenting inefficient code.
[quoted text clipped - 4 lines]
>
> I guess the world should wait for you to answer all of their questions...

He doesn't have the answers, that is why he is so anally pedantic at times.

>>--
>> © John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk
>
> Perhaps you should spend a little more time working on your social skills

He would have to gain some social skills before he could work on them.

Signature

Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly

Dr John Stockton - 31 Aug 2005 22:36 GMT
JRS:  In article <P-ednZ2dnZ2HgXGnnZ2dnSCeiN6dnZ2dRVn-
yJ2dnZ0@comcast.com>, dated Tue, 30 Aug 2005 20:10:45, seen in
news:comp.lang.javascript, McKirahan <News@McKirahan.com> posted :

>[snip]
>
[quoted text clipped - 5 lines]
>
>Instead of addressing me why don't you just address the OP?

Because it was you who rushed in with a very low-grade piece of code, as
has been your habit elsewhere.

>All you had to say is "Here's a preferred approach.".
>
[quoted text clipped - 4 lines]
>
>I'm faulted for offering "a" solution when it is requested!

No, for presenting one which is bad, and which you should have realised
was likely to be so from your previous experience.

>Perhaps you should spend a little more time working on your social skills
>...

Well, you seem to think that I wish to beat you around the head with a
mallet; you are mistaken, as I would prefer to use a maul or beetle.
But it's near enough what I intended.  Soft words are liable to be
misunderstood by those of low ability.

Signature

© John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 ©
<URL:http://www.jibbering.com/faq/>  JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Evertjan. - 30 Aug 2005 15:12 GMT
Zvonko wrote on 30 aug 2005 in comp.lang.javascript:

> 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

var myStore = new Date()

Signature

Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

ASM - 31 Aug 2005 01:22 GMT
> 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

and if its clowk gives wrong time ?
why not simplier get server's time ?

 urerTime    = new Date();
 userDay     = userTime.getdate();
 userMonth   = userTime.getMonth()+1*1;
 userYear    = userTime.getFullYear();
 userHour    = userTime.getHours();
 userMinutes = userTime.getMinutes();
 userSeconds = userTime.getSeconds();
 if(userMonth<10)   userMonth   = '0'+userMonth;
 if(userHour<10)    userHour    = '0'+userHour;
 if(userMinutes<10) userMinutes = '0'+userMinutes;
 if(userSeconds<10) userSeconds = '0'+userSeconds;

 userTime_us = userYear+'/'+userMonth+'/'+userDay+' - '+
               userHour+':'+userMinutes+':'+userSeconds;

 userTime_fr = userDay+'/'+userMonth+'/'+userYear+' - '+
               userHour+':'+userMinutes+':'+userSeconds;

Signature

Stephane Moriaux et son [moins] vieux Mac

Jim Davis - 31 Aug 2005 05:41 GMT
> 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/Arti
cles/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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.