473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Storing client date and time

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
Aug 30 '05 #1
8 3345
"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
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>
Aug 30 '05 #2
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()

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

Aug 30 '05 #3
JRS: In article <xL********************@comcast.com>, dated Tue, 30 Aug
2005 08:36:13, seen in news:comp.lang.javascript, McKirahan
<Ne**@McKirahan.com> posted :
"Zvonko" <zv****@velepromet.hr> wrote in message
news:df**********@ss405.t-com.hr...
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;
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();


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.

--
© 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.
Aug 30 '05 #4
ASM
Zvonko wrote:
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;
--
Stephane Moriaux et son [moins] vieux Mac
Aug 31 '05 #5
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:CX**************@merlyn.demon.co.uk...

[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
....
Aug 31 '05 #6

"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
Aug 31 '05 #7
McKirahan said the following on 8/30/2005 9:10 PM:
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:CX**************@merlyn.demon.co.uk...

[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.".


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.

[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...


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 31 '05 #8
JRS: In article <P-ednZ2dnZ2HgXGnnZ2dnSCeiN6dnZ2dRVn-
yJ*****@comcast.com>, dated Tue, 30 Aug 2005 20:10:45, seen in
news:comp.lang.javascript, McKirahan <Ne**@McKirahan.com> posted :
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:CX**************@merlyn.demon.co.uk...

[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?


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


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.

--
© 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.
Aug 31 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: m3ckon | last post by:
HI there, I currently store the date using the getdate() function but how can I store just the time or seperate the time off from a datetime datatype? M3ckon *** Sent via Devdex...
2
by: Robert | last post by:
I have no problem storing dates + times in a System.DateTime object. In addition, it's easy to output a Time as a string from an existing Date/Time. But I'm having trouble storing a time only. ...
4
by: brfin999 | last post by:
Server time zone 1 hour different from Client time zone. .Net 1.1 c# Win Forms app: actual date 9/25/04 displays 9/24/04 11:00 PM. When I change client time zone to equal server time zone, date...
11
by: James Hallam | last post by:
I have read through all the past topics and couldn't find what I was after so... I am looking to store some calculated values (don't flame just yet, just read on!). I have an piece of code...
3
by: john | last post by:
I am using MS Sql Express as the backend database for a Microsoft Access front end application. I am using a calendar control to store the desired date in the sql datetime field. The time...
7
by: fauxanadu | last post by:
Is it possible to store dates before 01/01/0100 A.D. (such as for as database storing world events would require) using MS Access? Verbose Explination I need to be able to store dates before...
1
by: suresh_nsnguys | last post by:
Hi, I am new to this Web dowmain.I am little bit confused with this functionality.I am looking forward some sugggesstion from you. Issue: Users allowed to create Ads in my application...
11
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I have worked with application settings in VS2005 and C# for awhile, but usually with standard types. I have been trying to store a custom container/class/type in an application setting and I have...
4
by: John A Grandy | last post by:
What are some best practices for storing pure dates and pure times in .NET ? I notice that DateTime.TimeOfDay() returns type TimeSpan , which is certainly sufficient for storing pure times , but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.