Solved

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

Posted on 2001-06-21
27
235 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
  • 8
  • 8
  • 6
  • +3
27 Comments
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 18

Accepted Solution

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

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

BRUNO
0
 

Expert Comment

by:m02759
Comment Utility
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
Comment Utility
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
Comment Utility
Any feedback b6106b?

:o)

Ant
0
 

Author Comment

by:b6106b
Comment Utility
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
Comment Utility
try this...

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


BRUNO
0
 

Author Comment

by:b6106b
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:bruno
Comment Utility
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
Comment Utility
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
Comment Utility
hmm...and stop pushing submit...  :-)
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
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
Comment Utility
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
Comment Utility
Any feedback on this b6106b?

Ant
0
 

Expert Comment

by:teacher_mod
Comment Utility
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
Comment Utility
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
Comment Utility
a.marsh, myself, and m02759 all posted possible solutions.  do as you see fit.

:-)

BRUNO
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
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
Comment Utility
teacher_mod must have forgotten about us...
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
Has resigned AFAIK

Cd&
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
Split or 50 points for each of us?

:o)

Ant
0
 
LVL 18

Expert Comment

by:bruno
Comment Utility
50 for each?  :-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now