javascript convert date to yyyy-mm-dd
snippet in javascript

javascript convert date to yyyy-mm-dd

user2719

// `date` is a `Date` object
const formatYmd = date => date.toISOString().slice(0, 10);

// Example
formatYmd(new Date());      // 2020-05-06

js date format yyyy-mm-dd

user8430

yourDate.toISOString().split('T')[0]

java script print date in YYYY-MM-DD format

user6702

let date_ob = new Date();

// adjust 0 before single digit date
let date = ("0" + date_ob.getDate()).slice(-2);

// current month
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);

// current year
let year = date_ob.getFullYear();

// prints date in YYYY-MM-DD format
console.log(year + "-" + month + "-" + date);