Solved

How to make form field default to today's date and time?

Posted on 2001-06-21
27
240 Views
Last Modified: 2010-04-09
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
Comment
Question by:b6106b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
  • 6
  • +3
27 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6214421
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6214441
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6214454
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Accepted Solution

by:
bruno earned 50 total points
ID: 6219135
You could use ASP to do this as well.

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

BRUNO
0
 

Expert Comment

by:m02759
ID: 6220782
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
 

Expert Comment

by:m02759
ID: 6220784
woops! I missed a quotation mark there:



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

maz
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6220994
Any feedback b6106b?

:o)

Ant
0
 

Author Comment

by:b6106b
ID: 6224970
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
 
LVL 18

Expert Comment

by:bruno
ID: 6224977
try this...

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


BRUNO
0
 

Author Comment

by:b6106b
ID: 6224996
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
 

Author Comment

by:b6106b
ID: 6225003
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
 

Author Comment

by:b6106b
ID: 6225005
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
 

Author Comment

by:b6106b
ID: 6225019
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
 
LVL 18

Expert Comment

by:bruno
ID: 6225026
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
 

Author Comment

by:b6106b
ID: 6225027
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
 
LVL 18

Expert Comment

by:bruno
ID: 6225032
hmm...and stop pushing submit...  :-)
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6225601
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
 
LVL 18

Expert Comment

by:bruno
ID: 6225701
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6282356
Any feedback on this b6106b?

Ant
0
 

Expert Comment

by:teacher_mod
ID: 6374567
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6375186
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
 
LVL 18

Expert Comment

by:bruno
ID: 6375866
a.marsh, myself, and m02759 all posted possible solutions.  do as you see fit.

:-)

BRUNO
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6520829
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
 
LVL 18

Expert Comment

by:bruno
ID: 6520856
teacher_mod must have forgotten about us...
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6520951
Has resigned AFAIK

Cd&
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6521006
Split or 50 points for each of us?

:o)

Ant
0
 
LVL 18

Expert Comment

by:bruno
ID: 6523685
50 for each?  :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP sending two datas 2 30
Can't get cursor to change 3 21
Jquery HTML() with ajax... replacing content 2 53
body or div size relative to background image 7 15
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question