date auto add 0
by Arvind Pal
JavaScript
// Input date in "1/1/2001" format
const inputDate = "12/33/2000";
// Split the input date into day, month, and year
const [month, day, year] = inputDate.split("/");
console.log(day, month, year)
// Create a new Date object with the year, month (0-based), and day
const date = new Date(`${month}/${day}/${year}`);
console.log("date", date)
// Format the date as "01/01/2001"
let formattedDate = ""
if (date != "Invalid Date") {
formattedDate = `${date.getDate().toString().padStart(2, "0")}/${(date.getMonth() + 1).toString().padStart(2, "0")}/${date.getFullYear()}`;
} else {
formattedDate = inputDate
}
console.log("formate", formattedDate); // Output: "01/01/2001"