JSFiddle - React, Tailwind, and code Playground
HTML
<h1 id="title">undefined date </h1>
<input type="submit" id="byBtn" value="Change" onclick="change2()"/>
JavaScript
function change2()
{
// Attempt to insert text at the cursor position. If insertion returns null,
// then the cursor's containing element doesn't allow text insertions.
var month = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
var d = new Date();
var dd = d.getDate();
dd = pad(dd, 2);
var mm = d.getMonth();
var yyyy = d.getFullYear();
var datec = dd + "-" + month[mm] + "-" + yyyy;
alert(datec);//without pad
mm = pad(mm,2);
var datec = dd + "-" + month[mm] + "-" + yyyy;
alert(datec);//with pad
}
function pad(str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}