9

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.

2
  • 1
    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. Commented Jun 16, 2009 at 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+) ,... . Commented Jun 13, 2012 at 6:02

11 Answers 11

19

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Only works if the input is not a HTML5 date input field.
9
<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

Comments

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>

1 Comment

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?
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(); 

Comments

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.

Comments

1

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"

1 Comment

This would depend on the server's timezone, not really user's local time
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();

Comments

0
<input id="date" name="date" />
<script type="text/javascript">
document.getElementById("date").value = new Date();
</script>

Comments

0

Another option is:

<script language="JavaScript" type="text/javascript"> 
document.getElementById("my_date:box").value = ( Stamp.getDate()+"/"+(Stamp.getMonth() + 1)+"/"+(Stamp.getYear()) );
</script>

Comments

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();

Comments

0
function onDatepicker(){
    let dateValue = document.getElementById('datepicker').value;
    if(dateValue.length == 2){
        document.getElementById('datepicker').value = dateValue+'/';
    }else if(dateValue.length > 2){
        let value1= dateValue.split('/');
        if(value1[1].length == 2 && value1.length == 2){
            document.getElementById('datepicker').value = value1[0]+'/'+value1[1]+'/';
        }
        if(value1.length > 2){
            if(value1[2].length == 1 || value1[2].length < 5){
                    document.getElementById('datepicker').value = value1[0]+'/'+value1[1]+'/'+value1[2];
                }else{
                    document.getElementById('datepicker').value = value1[0]+'/'+value1[1]+'/'+value1[2][0]+value1[2][1]+value1[2][2]+value1[2][3];
                }
            }
    }
}

2 Comments

Your answer is incomplete. You need to explain your code, so visitors of the question will be informed about what your idea is and why it is solving the problem.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.