function getFormattedDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // getMonth()返回的月份是从0开始的,所以需要+1
const day = String(date.getDate()).padStart(2, '0');
return `${year}(${year})-${month}(月)-${day}(日)`;
}
var todays = new Date();
var st tomorrow = new Date();
tomorrow.setDate(todays .getDate() + 1);
console.log(getFormattedDate(todays )); // 输出今天的日期,格式化后
console.log(getFormattedDate(tomorrow)); // 输出明天的日期,格式化后
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交