JS設定時間格式為2010-01-01,以及時間加減

请叫我七哥發表於2024-05-01
function formatDate(date){  
let year = date.getFullYear();
let month = date.getMonth()+1;
let day = date.getDate();
month = month < 10 ?'0'+ month : month;
day = day < 10 ?'0'+ day : day; return year +'-'+ month +'-'+ day;
}console.log(formatDate(new Date()));

//加7天 
var d = new Date();
d.setDate(d.getDate() + 7);
 $("#txtEndTime").val(formatDate(d));
//加一年 
var d = new Date();
d.setFullYear(d.getFullYear()+1);
$(
"#txtEndTime").val(formatDate(d));

相關文章