- 정보공유
[기타] 자바스크립트 날짜 계산식 YYYY-MM-DD
function get_addday(add, mode) {
var sDate = '2020-03-10';
var add = parseInt(add);
var yy = parseInt(sDate.substr(0, 4), 10);
var mm = parseInt(sDate.substr(5, 2), 10);
var dd = parseInt(sDate.substr(8, 2), 10);
if (mode == 'd') {
d = new Date(yy, mm - 1, dd + add);
}
else if (mode == 'm') {
d = new Date(yy, mm - 1 + add, dd);
}
else if (mode == 'y') {
d = new Date(yy + add, mm - 1, dd);
}
d.setDate(d.getDate() - 1);
yy = d.getFullYear();
mm = d.getMonth() + 1; mm = (mm < 10) ? '0' + mm : mm;
dd = d.getDate(); dd = (dd < 10) ? '0' + dd : dd;
var todate = yy+'-'+mm+'-'+dd;
alert(todate);
}