• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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/
0
b6106b
Asked:
b6106b
  • 8
  • 8
  • 6
  • +3
1 Solution
 
a.marshCommented:
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
0
 
a.marshCommented:
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
0
 
a.marshCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
brunoCommented:
You could use ASP to do this as well.

<%=monthname(month(now)) & " " & day(now) & ", " & year(now)%>

BRUNO
0
 
m02759Commented:
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
0
 
m02759Commented:
woops! I missed a quotation mark there:



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

maz
0
 
a.marshCommented:
Any feedback b6106b?

:o)

Ant
0
 
b6106bAuthor Commented:
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?

0
 
brunoCommented:
try this...

<input type="text" name="Date" size="10" value="<%=monthname(month(now)) & " " & day(now) & ", " & year(now)%>">


BRUNO
0
 
b6106bAuthor Commented:
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?


0
 
b6106bAuthor Commented:
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>
----------------
0
 
b6106bAuthor Commented:
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>
----------------
0
 
b6106bAuthor Commented:
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>
----------------
0
 
brunoCommented:
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
0
 
b6106bAuthor Commented:
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>
----------------
0
 
brunoCommented:
hmm...and stop pushing submit...  :-)
0
 
a.marshCommented:
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
0
 
brunoCommented:
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
0
 
a.marshCommented:
Any feedback on this b6106b?

Ant
0
 
teacher_modCommented:
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


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

:o)

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

:-)

BRUNO
0
 
COBOLdinosaurCommented:
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&
0
 
brunoCommented:
teacher_mod must have forgotten about us...
0
 
COBOLdinosaurCommented:
Has resigned AFAIK

Cd&
0
 
a.marshCommented:
Split or 50 points for each of us?

:o)

Ant
0
 
brunoCommented:
50 for each?  :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 8
  • 8
  • 6
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now