js获取日期yyyy-MM-dd

var nowDate = new Date();//当前时间
var year = nowDate.getFullYear();//获取年
var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1): nowDate.getMonth() + 1;//获取月份,两位不够补0
var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();//获取日期,两位不够补0

var dateStr = year + "-" + month + "-" + day;

结果:
2020-07-31
2019-11-11

猜你喜欢

转载自blog.csdn.net/weixin_49456013/article/details/107706290