Reputation: 19
Hey guys my onClick Event doesn't put the date into the text input field.
<input class="span2" id="datum" name="datum" type="date">
<button class="btn" type="button" onclick="datum()">Heute</button>
<script type="text/javascript">
function datum() {
var datum = new Date();
var tag = datum.getDate();
var monat = datum.getMonth();
var jahr = datum.getFullYear();
var element = document.getElementById("datum");
element.innerHTML = tag + "." + monat + "." + jahr;
}
</script>
Upvotes: 0
Views: 1942
Reputation: 178285
.value
instead of .innerHTML
. type=date
to type=text
since you want to use a different format
Is there any way to change input type="date" format? The HTML5 date input specification [1] refers to the RFC3339 specification, which specifies a full-date format equal to: yyyy-mm-dd.
Like this
element.value = tag + "." + (monat+1) + "." + jahr;
function pad(num) {
return String("0" + num).slice(-2);
}
function heute() {
var datum = new Date();
var tag = datum.getDate();
var monat = datum.getMonth();
var jahr = datum.getFullYear();
var element = document.getElementById("datum");
element.value = pad(tag) + "." + pad(monat+1) + "." + jahr;
}
<input class="span2" id="datum" name="datum" type="text" placeholder="tt.mm.jjjj">
<button class="btn" type="button" onclick="heute()">Heute</button>
Upvotes: 1
Reputation: 19
<input class="span2" id="datum" name="datum" type="text" value="<?php echo date("d.m.Y"); ?>" required>
<button class="btn" type="button">Heute</button>
I did it with PHP :P
Upvotes: 0