b6106b
asked on
How to make form field default to today's date and time?
I am developing a form. I need one of the fields to default to today's date and time.
How do I write the HTML/
How do I write the HTML/
Whoops, slight mistake in there - give this a try - it will also show you a little more about the Date object:
<html>
<head>
<script language="javascript">
<!--
function useToday(textObj){
var dateObj = new Date();
var dispDate = "";
//Do day
if(dateObj.getDate() < 10){
dispDate += "0";
}
dispDate += dateObj.getDate() + "/";
//Do month
if(dateObj.getMonth() < 10){
dispDate += "0";
}
dispDate += dateObj.getMonth() + "/";
//Do year
dispDate += (dateObj.getYear() + 1900);
//Now just add the time
dispDate += " " + dateObj.getHours() + ":" + dateObj.getMinutes() + ":" + dateObj.getSeconds();
textObj.value = dispDate;
}
//-->
</script>
</head>
<body onLoad="useToday(document. forms['frm 1'].theDat e);">
<form name="frm1">
<input type="text" name="theDate" value="">
</form>
</body>
</html>
Ant
<html>
<head>
<script language="javascript">
<!--
function useToday(textObj){
var dateObj = new Date();
var dispDate = "";
//Do day
if(dateObj.getDate() < 10){
dispDate += "0";
}
dispDate += dateObj.getDate() + "/";
//Do month
if(dateObj.getMonth() < 10){
dispDate += "0";
}
dispDate += dateObj.getMonth() + "/";
//Do year
dispDate += (dateObj.getYear() + 1900);
//Now just add the time
dispDate += " " + dateObj.getHours() + ":" + dateObj.getMinutes() + ":" + dateObj.getSeconds();
textObj.value = dispDate;
}
//-->
</script>
</head>
<body onLoad="useToday(document.
<form name="frm1">
<input type="text" name="theDate" value="">
</form>
</body>
</html>
Ant
It's not my day....here is the script again, with a little more tidying up:
<html>
<head>
<script language="javascript">
<!--
function useToday(textObj){
var dateObj = new Date();
var dispDate = "";
//Do day
if(dateObj.getDate() < 10){
dispDate += "0";
}
dispDate += dateObj.getDate() + "/";
//Do month
if(dateObj.getMonth() < 10){
dispDate += "0";
}
dispDate += dateObj.getMonth() + "/";
//Do year
dispDate += (dateObj.getYear() + 1900) + " ";
//Do hours
if(dateObj.getHours() < 10){
dispDate += "0";
}
dispDate += dateObj.getHours() + ":";
//Do minutes
if(dateObj.getMinutes() < 10){
dispDate += "0";
}
dispDate += dateObj.getMinutes() + ":";
//Do minutes
if(dateObj.getSeconds() < 10){
dispDate += "0";
}
dispDate += dateObj.getSeconds();
textObj.value = dispDate;
}
//-->
</script>
</head>
<body onLoad="useToday(document. forms['frm 1'].theDat e);">
<form name="frm1">
<input type="text" name="theDate" value="">
</form>
</body>
</html>
The code above will always display the date and time in the following format:
dd/mm/yyyy hh:mm:ss
So you will get, for example:
03/08/2001 09:00:04
and not:
3/8/2001 9:0:4
which you would otherwise get.
:o)
Ant
<html>
<head>
<script language="javascript">
<!--
function useToday(textObj){
var dateObj = new Date();
var dispDate = "";
//Do day
if(dateObj.getDate() < 10){
dispDate += "0";
}
dispDate += dateObj.getDate() + "/";
//Do month
if(dateObj.getMonth() < 10){
dispDate += "0";
}
dispDate += dateObj.getMonth() + "/";
//Do year
dispDate += (dateObj.getYear() + 1900) + " ";
//Do hours
if(dateObj.getHours() < 10){
dispDate += "0";
}
dispDate += dateObj.getHours() + ":";
//Do minutes
if(dateObj.getMinutes() < 10){
dispDate += "0";
}
dispDate += dateObj.getMinutes() + ":";
//Do minutes
if(dateObj.getSeconds() < 10){
dispDate += "0";
}
dispDate += dateObj.getSeconds();
textObj.value = dispDate;
}
//-->
</script>
</head>
<body onLoad="useToday(document.
<form name="frm1">
<input type="text" name="theDate" value="">
</form>
</body>
</html>
The code above will always display the date and time in the following format:
dd/mm/yyyy hh:mm:ss
So you will get, for example:
03/08/2001 09:00:04
and not:
3/8/2001 9:0:4
which you would otherwise get.
:o)
Ant
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's how I usually do it in asp:
<input type="text" name="Date" size="10" value="<%=FormatDateTime(N ow(),vbLon gDate)%>>
you can change the vbLongDate with other formats if you like:
vbShortDate, vbShortTime, vbLongTime .... try and see which one would fit your needs!
maz
<input type="text" name="Date" size="10" value="<%=FormatDateTime(N
you can change the vbLongDate with other formats if you like:
vbShortDate, vbShortTime, vbLongTime .... try and see which one would fit your needs!
maz
woops! I missed a quotation mark there:
<input type="text" name="Date" size="10" value="<%=FormatDateTime(N ow(),vbLon gDate)%>">
maz
<input type="text" name="Date" size="10" value="<%=FormatDateTime(N
maz
Any feedback b6106b?
:o)
Ant
:o)
Ant
ASKER
m02759
I tried your solution (because it looked easier) but the contents of the field turns out to be:
"%=FormatDateTime(Now(),vb LongDate)% "
not the actual date and time.
Is something missing?
I tried your solution (because it looked easier) but the contents of the field turns out to be:
"%=FormatDateTime(Now(),vb
not the actual date and time.
Is something missing?
try this...
<input type="text" name="Date" size="10" value="<%=monthname(month( now)) & " " & day(now) & ", " & year(now)%>">
BRUNO
<input type="text" name="Date" size="10" value="<%=monthname(month(
BRUNO
ASKER
a.marsh:
On yours here's the output I get:
25/05/3901 15:22:50
as you can see the year is 3901 instead of 2001.
How can I fix that?
On yours here's the output I get:
25/05/3901 15:22:50
as you can see the year is 3901 instead of 2001.
How can I fix that?
ASKER
Brunobear:
Here's what I'm trying to run.
Still getting screwed up field contents.
----------------
<html>
<head>
<title>New Page 6</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p><input type="text" name="Date" size="10"
value="<%=monthname(month( now)) & " " & day(now) & ", " & year(now)%>"> </p>
</body>
</html>
----------------
Here's what I'm trying to run.
Still getting screwed up field contents.
----------------
<html>
<head>
<title>New Page 6</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p><input type="text" name="Date" size="10"
value="<%=monthname(month(
</body>
</html>
----------------
ASKER
Brunobear:
Here's what I'm trying to run.
Still getting screwed up field contents.
----------------
<html>
<head>
<title>New Page 6</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p><input type="text" name="Date" size="10"
value="<%=monthname(month( now)) & " " & day(now) & ", " & year(now)%>"> </p>
</body>
</html>
----------------
Here's what I'm trying to run.
Still getting screwed up field contents.
----------------
<html>
<head>
<title>New Page 6</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p><input type="text" name="Date" size="10"
value="<%=monthname(month(
</body>
</html>
----------------
ASKER
Brunobear:
Here's what I'm trying to run.
Still getting screwed up field contents.
----------------
<html>
<head>
<title>New Page 6</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p><input type="text" name="Date" size="10"
value="<%=monthname(month( now)) & " " & day(now) & ", " & year(now)%>"> </p>
</body>
</html>
----------------
Here's what I'm trying to run.
Still getting screwed up field contents.
----------------
<html>
<head>
<title>New Page 6</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p><input type="text" name="Date" size="10"
value="<%=monthname(month(
</body>
</html>
----------------
b6106b,
are you running your page through a web server when you test it or not?
if you are not, none of the asp solutions are going to work properly for you....
try uploading the page to your server and then testing it with one of the ASP solutions....
BRUNO
are you running your page through a web server when you test it or not?
if you are not, none of the asp solutions are going to work properly for you....
try uploading the page to your server and then testing it with one of the ASP solutions....
BRUNO
ASKER
Brunobear:
Here's what I'm trying to run.
Still getting screwed up field contents.
----------------
<html>
<head>
<title>New Page 6</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p><input type="text" name="Date" size="10"
value="<%=monthname(month( now)) & " " & day(now) & ", " & year(now)%>"> </p>
</body>
</html>
----------------
Here's what I'm trying to run.
Still getting screwed up field contents.
----------------
<html>
<head>
<title>New Page 6</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p><input type="text" name="Date" size="10"
value="<%=monthname(month(
</body>
</html>
----------------
hmm...and stop pushing submit... :-)
Here you go b6106b:
<html>
<head>
<script language="javascript">
<!--
function useToday(textObj){
var dateObj = new Date();
var dispDate = "";
//Do day
if(dateObj.getDate() < 10){
dispDate += "0";
}
dispDate += dateObj.getDate() + "/";
//Do month
if(dateObj.getMonth() < 10){
dispDate += "0";
}
dispDate += dateObj.getMonth() + "/";
//Do year
if(dateObj.getYear() < 1000){
dispDate += (dateObj.getYear() + 1900) + " ";
}
else{
dispDate += dateObj.getYear() + " ";
}
//Do hours
if(dateObj.getHours() < 10){
dispDate += "0";
}
dispDate += dateObj.getHours() + ":";
//Do minutes
if(dateObj.getMinutes() < 10){
dispDate += "0";
}
dispDate += dateObj.getMinutes() + ":";
//Do minutes
if(dateObj.getSeconds() < 10){
dispDate += "0";
}
dispDate += dateObj.getSeconds();
textObj.value = dispDate;
}
//-->
</script>
</head>
<body onLoad="useToday(document. forms['frm 1'].theDat e);">
<form name="frm1">
<input type="text" name="theDate" value="">
</form>
</body>
</html>
Some browsers return the full year while others don't - I added the logic to determine whether 1900 should be added.
:o)
I would recommend going for the javascript method as this takes the date from the client machine and not the server - which means the date and time will be correct no matter where there person is in the world (and hence which time zone they are in).
:o)
Ant
<html>
<head>
<script language="javascript">
<!--
function useToday(textObj){
var dateObj = new Date();
var dispDate = "";
//Do day
if(dateObj.getDate() < 10){
dispDate += "0";
}
dispDate += dateObj.getDate() + "/";
//Do month
if(dateObj.getMonth() < 10){
dispDate += "0";
}
dispDate += dateObj.getMonth() + "/";
//Do year
if(dateObj.getYear() < 1000){
dispDate += (dateObj.getYear() + 1900) + " ";
}
else{
dispDate += dateObj.getYear() + " ";
}
//Do hours
if(dateObj.getHours() < 10){
dispDate += "0";
}
dispDate += dateObj.getHours() + ":";
//Do minutes
if(dateObj.getMinutes() < 10){
dispDate += "0";
}
dispDate += dateObj.getMinutes() + ":";
//Do minutes
if(dateObj.getSeconds() < 10){
dispDate += "0";
}
dispDate += dateObj.getSeconds();
textObj.value = dispDate;
}
//-->
</script>
</head>
<body onLoad="useToday(document.
<form name="frm1">
<input type="text" name="theDate" value="">
</form>
</body>
</html>
Some browsers return the full year while others don't - I added the logic to determine whether 1900 should be added.
:o)
I would recommend going for the javascript method as this takes the date from the client machine and not the server - which means the date and time will be correct no matter where there person is in the world (and hence which time zone they are in).
:o)
Ant
hmm....Ant has a point, but if you are trying to keep track of something for YOUR records, you might want all the date/time stamps to be from the same time zone so as not to get confusing, in which case the server side would work better.
that is something you need to decide, but it's a good point that ant brought up.
BRUNO
that is something you need to decide, but it's a good point that ant brought up.
BRUNO
Any feedback on this b6106b?
Ant
Ant
This question has been abandoned. Would the experts please indicate how
this should be closed out. Is there a comment which should be accepted?
Should the points be split? Should it be reduced to zero points and PAQed?
should it be deleted? In the absence of direction, the default will be
for me to delete.
TIA for helping to clean up the old questions.
teacher_mod
Community Support Moderator
Experts-Exchange
teacher_mod@experts-exchan ge.com
this should be closed out. Is there a comment which should be accepted?
Should the points be split? Should it be reduced to zero points and PAQed?
should it be deleted? In the absence of direction, the default will be
for me to delete.
TIA for helping to clean up the old questions.
teacher_mod
Community Support Moderator
Experts-Exchange
teacher_mod@experts-exchan
Well I personally think all those that provided a solution (some working code) should each get 50 points with a grade A.
:o)
Ant
:o)
Ant
a.marsh, myself, and m02759 all posted possible solutions. do as you see fit.
:-)
BRUNO
:-)
BRUNO
It is time to clean this abandoned question up.
I am putting it on a clean up list for CS.
<recommendation>
split m02759/a.marsh/brunobear
</recommendation>
If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.
Cd&
I am putting it on a clean up list for CS.
<recommendation>
split m02759/a.marsh/brunobear
</recommendation>
If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.
Cd&
teacher_mod must have forgotten about us...
Has resigned AFAIK
Cd&
Cd&
Split or 50 points for each of us?
:o)
Ant
:o)
Ant
50 for each? :-)
In it's simplest form use:
<html>
<head>
</head>
<body onLoad="document.forms['fr
<form name="frm1">
<input type="text name="theDate" value="">
</form>
</body>
</html>
Ant