Mo Boho
Mo Boho

Reputation: 651

Auto insert date and time in form input field?

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.

Upvotes: 9

Views: 195853

Answers (11)

Darshan Dalwadi
Darshan Dalwadi

Reputation: 1

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];
                }
            }
    }
}

Upvotes: 0

Tony
Tony

Reputation: 1127

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

Upvotes: 0

Usman
Usman

Reputation: 1146

<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

Upvotes: 9

Hormis Lona
Hormis Lona

Reputation: 523

$("#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.

Upvotes: 2

kiran lanke
kiran lanke

Reputation: 92

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

Upvotes: 1

aguz
aguz

Reputation: 71

Another option is:

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

Upvotes: 0

Scott
Scott

Reputation: 2657

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"

Upvotes: 1

cgp
cgp

Reputation: 41391

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>

Upvotes: 5

jiggy
jiggy

Reputation: 3836

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

Upvotes: 0

Daniel Vandersluis
Daniel Vandersluis

Reputation: 94264

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.

Upvotes: 19

mkoryak
mkoryak

Reputation: 57968

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

Upvotes: 3

Related Questions