Auto insert Date in form input field

D

Dwalker

I just want to enter today's date in a text input box with the format
mm/dd/yyyy when it receives focus. I've been playing around with this but
just can't seem to get it to work.

Any help would be appreciated.

Doug
 
L

Lee

Dwalker said:
I just want to enter today's date in a text input box with the format
mm/dd/yyyy when it receives focus. I've been playing around with this but
just can't seem to get it to work.

<html>
<head>
<script type="text/javascript">
function zp(n){
return n<10?("0"+n):n;
}
function insertDate(t,format){
var now=new Date();
var DD=zp(now.getDate());
var MM=zp(now.getMonth()+1);
var YYYY=now.getFullYear();
var YY=zp(now.getFullYear()%100);
format=format.replace(/DD/,DD);
format=format.replace(/MM/,MM);
format=format.replace(/YYYY/,YYYY);
format=format.replace(/YY/,YY);
t.value=format;
}
</script>
</head>
<body>
<form>
<input onfocus="insertDate(this,'MM/DD/YYYY')"><br>
<input onfocus="insertDate(this,'MM/DD/YY')"><br>
<input onfocus="insertDate(this,'DD/MM')"><br>
<input onfocus="insertDate(this,'YYYY-MM-DD')"><br>
</form>
</body>
</html>
 
D

Dwalker

Lee said:
Dwalker said:

<html>
<head>
<script type="text/javascript">
function zp(n){
return n<10?("0"+n):n;
}
function insertDate(t,format){
var now=new Date();
var DD=zp(now.getDate());
var MM=zp(now.getMonth()+1);
var YYYY=now.getFullYear();
var YY=zp(now.getFullYear()%100);
format=format.replace(/DD/,DD);
format=format.replace(/MM/,MM);
format=format.replace(/YYYY/,YYYY);
format=format.replace(/YY/,YY);
t.value=format;
}
</script>
</head>
<body>
<form>
<input onfocus="insertDate(this,'MM/DD/YYYY')"><br>
<input onfocus="insertDate(this,'MM/DD/YY')"><br>
<input onfocus="insertDate(this,'DD/MM')"><br>
<input onfocus="insertDate(this,'YYYY-MM-DD')"><br>
</form>
</body>
</html>
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
I just want to enter today's date in a text input box with the format
mm/dd/yyyy when it receives focus. I've been playing around with this but
just can't seem to get it to work.

That format should nor be used on the World-Wide Web; since only a few
countries use it, it is generally misleading (as is dd/mm/yyyy, of
course).
 
L

Lee

Dr John Stockton said:
JRS: In article <[email protected]>, seen in


That format should nor be used on the World-Wide Web; since only a few
countries use it, it is generally misleading (as is dd/mm/yyyy, of
course).

Curiosity: How widely accepted is yyyy-mm-dd out there in the Rest of
The World? In addition to software development, I've been using it on
bank drafts and forms for several months, but have never spotted it in
the wild (USA-AZ).
 
I

Ivo

Dr John Stockton said:
JRS: In article <[email protected]>, seen in


That format should nor be used on the World-Wide Web; since only a few
countries use it, it is generally misleading (as is dd/mm/yyyy, of
course).

Things became confusing only after the colonists in the New World went
looking for an identity in spelling anomalies.
No 'countries' that I know of use the format yyyy/mm/dd although that is the
one which sorts alphabetically and chronologically simultaneously. Or would
you suggest using the Julian Day Number?
Ivo
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Things became confusing only after the colonists in the New World went
looking for an identity in spelling anomalies.

Any responsibility that the UK may have for this must be shared in part
with the country that founded New Amsterdam. It is one thing, I
suspect, for which the French need not be blamed.
No 'countries' that I know of use the format yyyy/mm/dd although that is the
one which sorts alphabetically and chronologically simultaneously.

I believe Japan and China use it routinely. MSDOS 5 believes that YYYY-
MM-DD is also used in French Canada, Hungary, Czechoslovakia, Sweden,
Yugoslavia, Poland.
Or would
you suggest using the Julian Day Number?

No, not least because the Julian Day Number does not define a Civil
Date. Modified JD defines a UK Civil Date (ignoring Summer Time); and
Chronological JD & CMJD define a civil date anywhere.

For more, see via below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,656
Members
45,397
Latest member
SabineWher
Top