Link to home
Start Free TrialLog in
Avatar of b6106b
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/
Avatar of a.marsh
a.marsh

You cannot do it with just HTML. One way is to use Javascript:

In it's simplest form use:

<html>
<head>
</head>
<body onLoad="document.forms['frm1'].theDate.value = new Date();">
<form name="frm1">
<input type="text name="theDate" value="">
</form>
</body>
</html>

Ant
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['frm1'].theDate);">
<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['frm1'].theDate);">
<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
Avatar of bruno
bruno
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's how I usually do it in asp:

<input type="text" name="Date" size="10" value="<%=FormatDateTime(Now(),vbLongDate)%>>

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(Now(),vbLongDate)%>">

maz
Any feedback b6106b?

:o)

Ant
Avatar of b6106b

ASKER

m02759
I tried your solution (because it looked easier) but the contents of the field turns out to be:
"%=FormatDateTime(Now(),vbLongDate)%"
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
Avatar of b6106b

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?


Avatar of b6106b

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>
----------------
Avatar of b6106b

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>
----------------
Avatar of b6106b

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>
----------------
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
Avatar of b6106b

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>
----------------
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['frm1'].theDate);">
<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
Any feedback on this b6106b?

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-exchange.com


Well I personally think all those that provided a solution (some working code) should each get 50 points with a grade A.

:o)

Ant
a.marsh, myself, and m02759 all posted possible solutions.  do as you see fit.

:-)

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&
teacher_mod must have forgotten about us...
Has resigned AFAIK

Cd&
Split or 50 points for each of us?

:o)

Ant
50 for each?  :-)