0

i did convert value from date input to text input (in html) using javascript to change the format from mm/dd/yyyy to dd/mm/yyyy. and it works. but the value become 2/12/2017 from 12/02/2017.
i need it to become 02/12/2017 (not 2/12/2017). how to put the '0' for date under 10?
here's my code:

function mydate1()
{
    d=new Date(document.getElementById("date").value); //fetching date input's value
    dt=d.getDate(); //fetching date
    mn=d.getMonth(); //fetching month
    mn++;
    yy=d.getFullYear(); //fetching year
    document.getElementById("ndt").value=dt+"/"+mn+"/"+yy //inserting value to text input
    document.getElementById("ndt").hidden=false;
    document.getElementById("dt").hidden=true;}

1 Answers1

0

I use this trick:

("0" + d.getDate()).slice(-2)

and for the month:

("0" + (d.getMonth() + 1)).slice(-2)
emish89
  • 565
  • 8
  • 23