0

So I created a date input field where the field box displays the date, but as default it displays (mm/dd/yyyy) and I was wondering if you can replace this with the current date...

I've looked at other questions and it didn't really help much. I put this block of code under the <script> tag. And it still didn't replace the (mm/dd/yyyy) text in the field. How can I do this or if it is possible.

function getTodaysDate() {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10){
         dd='0'+dd
    } 
    if(mm<10){
         mm='0'+mm
    } 
    var today = dd+'/'+mm+'/'+yyyy;
    document.write(today);
}

<p>
   Date: <input type="date" name="currentDate" id="currentDate" value="currentDate();"/>
</p>
user3851283
  • 337
  • 1
  • 3
  • 14

1 Answers1

0

try this (Pure JS Solution)

<p>
  Date:
  <input type="date" name="currentDate" id="currentDate" />
</p>
<script>
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!

  var yyyy = today.getFullYear();
  if (dd < 10) {
    dd = '0' + dd
  }
  if (mm < 10) {
    mm = '0' + mm
  }
  var today = dd + '/' + mm + '/' + yyyy;
  document.getElementById('currentDate').value = today;
</script>
J Santosh
  • 3,808
  • 2
  • 22
  • 43