0

Good morning everyone. I have a little problem formatting a date.

I would like to make sure, with Javascript, that today's date is shown by default in an html <input type="date>" in this format: "dd-mm-yyyy". I managed to do this by changing the input to <input type="text">, the problem is that this way the automatic date picker that exists when the input is <input type="date"> no longer works.

So I would like to keep the <input type="date"> to keep the default datepicker, but show the default date same as today's, in this "dd-mm-yyyy" format.

How can I do? Thanks a lot to everyone. Forgive my bad English.

I tried with this portion of code: let today= new Date(); let day = oggi.getDate(); let month = oggi.getMonth() + 1; let year = oggi.getFullYear();

if(day < 10) giorno = '0' + giorno; if(month < 10) mese = '0' + mese;

let formattedDate = day + '-' + month + '-' + year;

document.getElementById('DATE').value = formattedDate;

now this code function only if the input type in html is <input type="text">, unfortunately in this way, the datepicker present when input <input type="date"> is not displayed.

If i change the html <input type="date">, the default today date doesn't be shown on the input that remains empty.

Thanks again.

GM02
  • 15
  • 4
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Please [**search thoroughly**](/search?q=%5Bhtml%5D+set+date+format+on+input) before posting. More about searching [here](/help/searching). – T.J. Crowder Mar 16 '23 at 13:41
  • Please may you share a [mcve] of the issue besides the description? It's good what you've got, but you seem to be missing details a working example would show – evolutionxbox Mar 16 '23 at 13:45

0 Answers0