first commit
This commit is contained in:
60
src/utils/dateUtils.js
Normal file
60
src/utils/dateUtils.js
Normal file
@@ -0,0 +1,60 @@
|
||||
function addMonthsToDate(dateString, monthsToAdd) {
|
||||
const date = new Date(dateString);
|
||||
date.setMonth(date.getMonth() + monthsToAdd);
|
||||
date.setDate(date.getDate() - 1);
|
||||
return formatDate(date);
|
||||
}
|
||||
|
||||
function dateFormatter(dateString) {
|
||||
if (!dateString || !/^\d{4}-\d{2}-\d{2}$/.test(dateString)) return "날짜 형식 오류";
|
||||
const [year, month, day] = dateString.split('-');
|
||||
return `${year}년 ${month}월 ${day}일`;
|
||||
}
|
||||
|
||||
function calculateDaysFromNow(dateString, now = new Date()) {
|
||||
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
||||
today.setHours(0,0,0,0);
|
||||
const inputDate = new Date(dateString);
|
||||
inputDate.setHours(0,0,0,0);
|
||||
return Math.ceil((inputDate.getTime() - today.getTime()) / 86400000);
|
||||
}
|
||||
|
||||
function calculateDateDifference(date1Str, date2Str) {
|
||||
const d1 = new Date(date1Str);
|
||||
d1.setHours(0, 0, 0, 0);
|
||||
const d2 = new Date(date2Str);
|
||||
d2.setHours(0, 0, 0, 0);
|
||||
return Math.ceil(Math.abs(d2.getTime() - d1.getTime()) / 86400000);
|
||||
}
|
||||
|
||||
function calculateProgress(startDateStr, endDateStr, now = new Date()) {
|
||||
const start = new Date(startDateStr);
|
||||
start.setHours(0, 0, 0, 0);
|
||||
const end = new Date(endDateStr);
|
||||
end.setHours(0, 0, 0, 0);
|
||||
const currentTime = now.getTime();
|
||||
if (start.getTime() >= end.getTime() || currentTime >= end.getTime()) return '100.0000000';
|
||||
if (currentTime <= start.getTime()) return 0;
|
||||
return Math.max(0, Math.min(100, ((currentTime - start.getTime()) / (end.getTime() - start.getTime())) * 100));
|
||||
}
|
||||
|
||||
function calculateMonthDifference(sYear, sMonth, eYear, eMonth) {
|
||||
return (Number(eYear) - Number(sYear)) * 12 + (Number(eMonth) - Number(sMonth));
|
||||
}
|
||||
|
||||
function formatDate(date) {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addMonthsToDate,
|
||||
dateFormatter,
|
||||
calculateDaysFromNow,
|
||||
calculateDateDifference,
|
||||
calculateProgress,
|
||||
calculateMonthDifference,
|
||||
formatDate
|
||||
};
|
Reference in New Issue
Block a user