How to convert only the date without time to a string in JavaScript

1 Answer

0 votes
// Convert a Date to a string (YYYY-MM-DD)
function dateToString(d) {
    return d.toISOString().slice(0, 10); // ISO format, date only
}

// Build a Date from integers (year, month, day)
function makeDate(y, m, d) {
    return new Date(y, m - 1, d); // JS months are 0-based
}

// 1. Today's date
const today = new Date();
console.log("Today's date is:", dateToString(today));

// 2. Hard-coded date
const myDate = makeDate(2025, 12, 7);
console.log("Hard-coded date is:", dateToString(myDate));



/*
run:

Today's date is: 2026-05-30
Hard-coded date is: 2025-12-07

*/

 



answered 8 hours ago by avibootz
...