function lpad(str, padLen, padStr) {
if (padStr.length > padLen) {
return str;
}
str += ""; // 문자로
padStr += ""; // 문자로
while (str.length < padLen)
str = padStr + str;
str = str.length >= padLen ? str.substring(0, padLen) : str;
return str;
}
/**
* Option :
* - format Default YYYY-mm-dd
* - minusYear Default 0
* - minusMonth Default 0
* - minusDay Default 0
* - minusDay Default 0
* - value Default current time
*/
function fnGetDate(option){
const _date = new Date()
, _format = option[`format`] ?? 'YYYY-mm-dd'
, _minusYear = option[`minusYear`] ?? 0
, _minusMonth = option[`minusMonth`] ?? 0
, _minusDay = option[`minusDay`] ?? 0;
_date.setFullYear(_date.getFullYear() - _minusYear);
_date.setMonth(_date.getMonth() - _minusMonth);
_date.setDate(_date.getDate() - _minusDay);
const _year = _date.getFullYear()
, _month = lpad(_date.getMonth()+1 ,2,'0')
, _day = lpad(_date.getDate() ,2,'0')
, _hour = lpad(_date.getHours(),2,'0')
, _minute = lpad(_date.getMinutes(),2,'0');
return _format.replace('YYYY',_year)
.replace('mm',_month)
.replace('dd',_day)
.replace('HH',_hour)
.replace('MM',_minute);
}
function fnSetDate(option){
const _target = option[`target`] ?? []
, _selector = option[`selector`]
, _value = option[`value`]
, _format = option[`format`]
, _el = _target.length != 0 ? [_target] : document.querySelectorAll(_selector);
if( _el == undefined || _el == null || _el.length < 1)
return console.error(`타겟 엘리먼트가 존재하지 않습니다`);
_el.forEach(el=>{
console.log(el);
el.value = _value ?? fnGetDate({format : _format});
})
}