1

I want to insert a current date into an input in form with javascript, but it is not working, here are my codes:

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("registrationday").value = getDay()

  function getDay() {
    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0');
    var yyyy = today.getFullYear();

    today =  "\"" + yyyy + '-' + mm + '-' + dd + "\"";
    return today
  }
})
<input type="date" id="registrationday" name="registrationday" disabled>
Spectric
  • 30,714
  • 6
  • 20
  • 43
Batman
  • 61
  • 5
  • 2
    There is NO need to use a custom function for what you are trying to achieve. just use this `document.getElementById('registrationday').valueAsDate = new Date();` – Always Helping Oct 16 '21 at 23:54

4 Answers4

2

Remove the quotes:

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("registrationday").value = getDay()

  function getDay() {
    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0');
    var yyyy = today.getFullYear();

    today =  yyyy + '-' + mm + '-' + dd;
    return today
  }
})
<input type="date" id="registrationday" name="registrationday" disabled>

However, a better solution is just to assign the current date to the input's valueAsDate property (suggested by @Always Helping):

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("registrationday").valueAsDate = getDay()

  function getDay() {
    return new Date()
  }
})
<input type="date" id="registrationday" name="registrationday" disabled>
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • 4
    why NOT -> `document.getElementById('registrationday').valueAsDate = new Date();` - keep it simple ayyy. – Always Helping Oct 16 '21 at 23:57
  • 2
    @AlwaysHelping Yes, that's certainly a better solution. I was simply pointing out what was wrong with OP's solution. – Spectric Oct 16 '21 at 23:58
2
var currentDate= new Date();

var day = ("0" + currentDate.getDate()).slice(-2);
var month = ("0" + (currentDate.getMonth() + 1)).slice(-2);

var today = currentDate.getFullYear()+"-"+(month)+"-"+(day) ;

document.getElementById("datepicker").value = today;
Soufiane
  • 160
  • 10
1

Solved with this JS:

document.addEventListener("DOMContentLoaded", function(){
var date = new Date ();
var day = datum.getDate(),
    month = datum.getMonth() + 1,
    year = datum.getFullYear();
var today = day + "-" + month + "-" + day ;

document.getElementById('registrationday').value = today;  
});

HTML:

<input type="date" id="registrationday" name="registrationday" disabled>
Batman
  • 61
  • 5
0

Your function returns "\"2021-10-17\"" which is invalid format (unnecessary slash, and double quote)

Just change "\"" + yyyy + '-' + mm + '-' + dd + "\"";

To: today = yyyy + '-' + mm + '-' + dd; at it will return correct output 2021-10-17

user3075373
  • 44
  • 1
  • 4
  • 19