sk_fems_ui commit
This commit is contained in:
161
plugins/dateUtility.js
Normal file
161
plugins/dateUtility.js
Normal file
@ -0,0 +1,161 @@
|
||||
const dayjs = require('dayjs');
|
||||
|
||||
const DateUtility = () => {
|
||||
/*
|
||||
// *
|
||||
// * @param {*} date 기준일
|
||||
// * @param {*} val 계산일수
|
||||
// * @param {*} type 날짜 포멧
|
||||
// * @returns
|
||||
*/
|
||||
const addDate = (val, type, date) => {
|
||||
let returnDt = null;
|
||||
const myDate = !date ? dayjs() : dayjs(date);
|
||||
returnDt = myDate.add(val || 0, 'day');
|
||||
if (type === 'd') returnDt = returnDt.format('YYYYMMDD');
|
||||
else if (type) returnDt = returnDt.format(type);
|
||||
return returnDt;
|
||||
};
|
||||
|
||||
/*
|
||||
// *
|
||||
// * @param {*} date 기준일
|
||||
// * @param {*} val 계산주수
|
||||
// * @param {*} type 날짜 포멧
|
||||
// * @returns
|
||||
*/
|
||||
const addWeek = (val, type, date) => {
|
||||
let returnDt = null;
|
||||
const myDate = !date ? dayjs() : dayjs(date);
|
||||
returnDt = myDate.add(val || 0, 'week');
|
||||
if (type === 'd') returnDt = returnDt.format('YYYYMMDD');
|
||||
else if (type) returnDt = returnDt.format(type);
|
||||
return returnDt;
|
||||
};
|
||||
|
||||
/*
|
||||
// *
|
||||
// * @param {*} date 기준일
|
||||
// * @param {*} val 계산월수
|
||||
// * @param {*} type 날짜 포멧
|
||||
// * @returns
|
||||
*/
|
||||
const addMonth = (val, type, date) => {
|
||||
let returnDt = null;
|
||||
const myDate = !date ? dayjs() : dayjs(date);
|
||||
returnDt = myDate.add(val || 0, 'month');
|
||||
//월말인 경우 월을 더할 경우 해당월의 마지막날로 설정되도록 처리
|
||||
if (
|
||||
myDate.format('YYYYMMDD') === myDate.endOf('month').format('YYYYMMDD')
|
||||
) {
|
||||
returnDt = returnDt.endOf('month');
|
||||
}
|
||||
if (type === 'd') returnDt = returnDt.format('YYYYMMDD');
|
||||
else if (type) returnDt = returnDt.format(type);
|
||||
return returnDt;
|
||||
};
|
||||
|
||||
/*
|
||||
// *
|
||||
// * @param {*} date 기준일
|
||||
// * @param {*} val 계산연수
|
||||
// * @param {*} type 날짜 포멧
|
||||
// * @returns
|
||||
*/
|
||||
const addYear = (val, type, date) => {
|
||||
let returnDt = null;
|
||||
const myDate = !date ? dayjs() : dayjs(date);
|
||||
returnDt = myDate.add(val || 0, 'year');
|
||||
if (type === 'd') returnDt = returnDt.format('YYYYMMDD');
|
||||
else if (type) returnDt = returnDt.format(type);
|
||||
return returnDt;
|
||||
};
|
||||
|
||||
/*
|
||||
// *
|
||||
// * @param {*} fromDt 시작일
|
||||
// * @param {*} toDt 종료일
|
||||
// * @param {*} type 기준
|
||||
// * @returns
|
||||
*/
|
||||
const diff = (fromDt, toDt, type) => {
|
||||
let returnDt = null;
|
||||
if (fromDt && toDt) {
|
||||
const myFromDt = fromDt === 'today' ? dayjs() : dayjs(fromDt);
|
||||
const myToDt = toDt === 'today' ? dayjs() : dayjs(toDt);
|
||||
let myType = 'days';
|
||||
switch (type) {
|
||||
case 'w':
|
||||
myType = 'weeks';
|
||||
break;
|
||||
case 'm':
|
||||
myType = 'months';
|
||||
break;
|
||||
case 'y':
|
||||
myType = 'year';
|
||||
break;
|
||||
case 'd':
|
||||
myType ='day';
|
||||
break;
|
||||
default:
|
||||
}
|
||||
returnDt = myToDt.diff(myFromDt, myType);
|
||||
}
|
||||
return returnDt;
|
||||
};
|
||||
|
||||
/*
|
||||
// *
|
||||
// * @param {*} d 기준일
|
||||
// * @param {*} f 날짜 포멧
|
||||
// * @returns
|
||||
*/
|
||||
const format = (d, f) => {
|
||||
let returnDt = null;
|
||||
const myDate = !d ? dayjs() : dayjs(d);
|
||||
const myFormat = f || 'YYYYMMDD';
|
||||
returnDt = myDate.format(myFormat);
|
||||
return returnDt;
|
||||
};
|
||||
|
||||
/*
|
||||
// *
|
||||
// * @param {*} strDate 기준일
|
||||
// * @returns
|
||||
*/
|
||||
const getDate = strDate => {
|
||||
const myDate = !strDate ? dayjs() : dayjs(strDate);
|
||||
return myDate
|
||||
.hour(0)
|
||||
.minute(0)
|
||||
.second(0);
|
||||
};
|
||||
|
||||
/*
|
||||
// *
|
||||
// * @param {*} date 기준일
|
||||
// * @param {*} type 날짜 포멧
|
||||
// * @returns
|
||||
*/
|
||||
const getLastDay = (type, date) => {
|
||||
let returnDt = null;
|
||||
const myDate = !date || date.length < 4 ? dayjs() : dayjs(date);
|
||||
returnDt = myDate.endOf('month');
|
||||
if (type === 'd') returnDt = returnDt.format('YYYYMMDD');
|
||||
else if (type) returnDt = returnDt.format(type);
|
||||
return returnDt;
|
||||
};
|
||||
|
||||
return {
|
||||
addDate, // 날짜 더하기
|
||||
addWeek, // 주 더하기
|
||||
addMonth, // 월 더하기
|
||||
addYear, // 년도 더하기
|
||||
diff, // 두 날짜의 차이 구하기
|
||||
format, // 날짜 값을 포맷팅하기
|
||||
getDate, // 스트링 날자 값을 data type로 변환하기
|
||||
getLastDay, // 해당 년/월의 마지막 날짜 구하기
|
||||
};
|
||||
};
|
||||
|
||||
export default DateUtility();
|
Reference in New Issue
Block a user