xxxxxxxxxx
function getDateString(datetime){
let d = datetime.getDate();
let m = datetime.getMonth();
return `${datetime.getFullYear()}-${m < 10 ? "0" + m : m}-${d < 10 ? "0" + d : d}`;
}
xxxxxxxxxx
// `date` is a `Date` object
const formatYmd = date => date.toISOString().slice(0, 10);
// Example
formatYmd(new Date()); // 2020-05-06
xxxxxxxxxx
const yourDate = new Date()
yourDate.toISOString().split('T')[0]
xxxxxxxxxx
const yourDate = new Date()
yourDate.toISOString().split('T')[0]
xxxxxxxxxx
const date = new Date();
const formattedDate = date.toISOString().slice(0, 10);
console.log(formattedDate);
// Output: 2023-05-07
xxxxxxxxxx
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
// Get the current date and time
const currentDate = new Date();
// Format the date using the defined function
const formattedDate = formatDate(currentDate);
console.log(formattedDate); // Example output: "2022-01-31 09:15:30"
xxxxxxxxxx
function pad2(n) {
return (n < 10 ? '0' : '') + n;
}
var date = new Date();
var month = pad2(date.getMonth()+1);//months (0-11)
var day = pad2(date.getDate());//day (1-31)
var year= date.getFullYear();
var formattedDate = day+"-"+month+"-"+year;
alert(formattedDate); //28-02-2021