xxxxxxxxxx
let d = '06/22/2022'.split('/')
d = d[2].concat(d[0], d[1])
console.log(typeof d, d)
// string 20220622
xxxxxxxxxx
// `date` is a `Date` object
const formatYmd = date => date.toISOString().slice(0, 10);
// Example
formatYmd(new Date()); // 2020-05-06
xxxxxxxxxx
var todayDate = new Date().toISOString()
console.log(todayDate);
xxxxxxxxxx
const yourDate = new Date()
yourDate.toISOString().split('T')[0]
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
xxxxxxxxxx
function convert(str) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), mnth, day].join("-");
}
console.log(convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)"))
//-> "2011-06-08"
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
const str = '22/04/2022';
const [day, month, year] = str.split('/');
console.log(day); // 22
console.log(month); // 04
console.log(year); // 2022
const date = new Date(+year, month - 1, +day);
console.log(date); // Fri Apr 22 2022