1

I've come up with using a function on page load:

function getDate() {
    let currentDate = new Date();
    let year = currentDate.getFullYear();
    let month = currentDate.getMonth() + 1;
    if (month < 10) {
        month = "0" + month
    }
    let day = currentDate.getDate();
    let today = year + '-' + month + '-' + day
    return today
}

Once the page loads, the return value is inserted into the value parameter. The function just looks really messy, is there a more concise way to do this?

Ari
  • 2,499
  • 2
  • 24
  • 54
  • You can use moment.js – Shubham Nagota Feb 24 '20 at 08:23
  • if you use moment check this https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – Madhawa Priyashantha Feb 24 '20 at 08:24
  • 1
    See the [second answer](https://stackoverflow.com/a/29774197/5648954) of the link above for a "simpler" solution (and [this](https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd/29774197#comment84587622_29774197) comment if you're experiencing issues) – Nick Parsons Feb 24 '20 at 08:24
  • 1
    @ShubhamNagota Not every date-related question should be answered with "use moment.js". This question most certainly does not need moment.js at all. – str Feb 24 '20 at 08:26

0 Answers0