Auto insert date and time in form input field?

Go To StackoverFlow.com

8

Using javascript, how can we auto insert the current DATE and TIME into a form input field.

I'm building a "Problems" form to keep track of user submitted complaints about a certain service. I'm collecting as much info as possible. I want to save the staff here from having to manually enter the date and time each time they open the form.

I have this snippet of code:

<input id="date" name="date" value="javascript:document.write(Date()+'.')"/>

but it's not working.

many thanks for any help.

2009-06-16 14:42
by Mo Boho
No attribute of any HTML element will ever execute a JS statement: HTML is not a scripting language. onclick et al just provide values to a DOM representation - annakata 2009-06-16 14:49
use server side code(PHP,ASP.NET) for more secure in this issue. the hackers(beginner) could be change your input values by FireBug,Developer Tools(IE8+) ,... - MajidTaheri 2012-06-13 06:02


18

Javascript won't execute within a value attribute. You could do something like this, though:

<input id="date" name="date">

<script type="text/javascript">
  document.getElementById('date').value = Date();
</script>

You'd probably want to format the date as you prefer, because the default output of Date() looks something like: Tue Jun 16 2009 10:47:10 GMT-0400 (Eastern Daylight Time). See this SO question for info about formatting a date.

2009-06-16 14:46
by Daniel Vandersluis
Only works if the input is not a HTML5 date input field - Oliver Moran 2016-09-27 10:42


8

<input type="date" id="myDate"  />

<script type="text/javascript">
function SetDate()
{
var date = new Date();

var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();

if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;

var today = year + "-" + month + "-" + day;


document.getElementById('myDate').value = today;
}
</script>

<body onload="SetDate();">

found here: http://jsbin.com/oqekar/1/edit?html,js,output

2015-03-30 04:13
by Usman


5

See the example, http://jsbin.com/ahehe

Use the JavaScript date formatting utility described here.

<input id="date" name="date" />

<script>
   document.getElementById('date').value = (new Date()).format("m/dd/yy");
</script>
2009-06-16 14:46
by cgp
Sorry to be a "Johnny come lately" but I have tried this script above seven ways from Sunday and can't get it to populate the field. I built a new file all that is in it is the code above with proper html tagging and it does not produce a text box with a date in it. Any suggestions - user1794918 2014-12-06 12:29


3

The fastest way to get date formatting thats any good is to use datejs.

The lib is really easy to use and does give you very pretty formatting.


as far as your code snippet, dont use document.write if you can help it. try

 document.getElementById("date").value = new Date().toUTCString(); 
2009-06-16 14:44
by mkoryak


2

$("#startDate").val($.datepicker.formatDate("dd/mm/yy", new Date()));
$("#endDate").val($.datepicker.formatDate("dd/mm/yy", new Date()));

Add the above code at the end of the script. This is required because the datepicker plugin has no provision to set the date in the control while initializing.

2015-01-21 04:56
by Hormis Lona


1

If you are using HTML5 date

use this code

HTML

<input type="date" name="bday" id="start_date"/>

Java Script

document.getElementById('start_date').value = Date();
2014-07-26 07:07
by kiran lanke


0

<input id="date" name="date" />
<script type="text/javascript">
document.getElementById("date").value = new Date();
</script>
2009-06-16 14:47
by jiggy


0

It sounds like you are going to be storing the value that input field contains after the form is submitted, which means you are using a scripting language. I would use it instead of JavaScript as most scripting languages have better time/date formatting options. In PHP you could do something like this:

<input id="date" name="date" value="<?php echo date("M j, Y - g:i"); ?>"/>

Which would fill the field with "Jun 16, 2009 - 8:58"

2009-06-16 14:59
by Scott
This would depend on the server's timezone, not really user's local tim - Grace Shao 2014-07-21 23:15


0

Another option is:

<script language="JavaScript" type="text/javascript"> 
document.getElementById("my_date:box").value = ( Stamp.getDate()+"/"+(Stamp.getMonth() + 1)+"/"+(Stamp.getYear()) );
</script>
2010-03-25 01:16
by aguz


0

var date = new Date();

document.getElementById("date").value = date.getFullYear() + "-" + (date.getMonth()<10?'0':'') + (date.getMonth() + 1) + "-" + (date.getDate()<10?'0':'') + date.getDate();

document.getElementById("hour").value = (date.getHours()<10?'0':'') + date.getHours()  + ":" + (date.getMinutes()<10?'0':'') + date.getMinutes();
2016-06-30 08:51
by Tony
Ads