phx-react
Version:
PHX REACT
156 lines • 6.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkDisabled = exports.checkActive = exports.getMinutes = exports.getHour = exports.dateStrToDate = exports.getMonthName = exports.createMonthCalendarWithAdjacentMonths = exports.formatDate = void 0;
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${day}-${month}-${year}`;
};
exports.formatDate = formatDate;
// Hàm để tạo mảng các ngày trong tháng với đúng vị trí, bao gồm cả các ngày của tháng trước và tháng sau
const createMonthCalendarWithAdjacentMonths = (year, month) => {
const getFirstDayOfMonth = (year, month) => {
const firstDay = new Date(year, month, 1);
const dayOfWeek = firstDay.getDay(); // 0 (Chủ Nhật) đến 6 (Thứ 7)
return { date: firstDay, dayOfWeek };
};
// Hàm để lấy số ngày trong tháng
const getDaysInMonth = (year, month) => {
return new Date(year, month + 1, 0).getDate();
};
const firstDayInfo = getFirstDayOfMonth(year, month);
const daysInMonth = getDaysInMonth(year, month);
const calendar = [];
// // Tính tháng trước và năm trước
// const previousMonth = month === 0 ? 11 : month - 1
// const previousYear = month === 0 ? year - 1 : year
// // Tính tháng sau và năm sau
// const nextMonth = month === 11 ? 0 : month + 1
// const nextYear = month === 11 ? year + 1 : year
// Thêm các ngày của tháng trước
// const daysInPreviousMonth = getDaysInMonth(previousYear, previousMonth)
// for (let i = firstDayInfo.dayOfWeek === 0 ? 6 : firstDayInfo.dayOfWeek - 1; i >= 1; i--) {
// calendar.push({
// date: formatDate(new Date(previousYear, previousMonth, daysInPreviousMonth - i + 1)),
// dayOfWeek: new Date(previousYear, previousMonth, daysInPreviousMonth - i + 1).getDay(),
// isAdjacentMonth: true,
// })
// }
// Thêm các ngày trong tháng
for (let day = 1; day <= daysInMonth; day++) {
calendar.push({
date: (0, exports.formatDate)(new Date(year, month, day)),
dayOfWeek: (firstDayInfo.dayOfWeek + day - 1) % 7,
isCurrentMonth: true,
isToday: (0, exports.formatDate)(new Date(year, month, day)) === (0, exports.formatDate)(new Date()),
});
}
// Thêm các ngày của tháng sau
// const lastDayInfo = getFirstDayOfMonth(nextYear, nextMonth)
// for (let i = 1; calendar.length < 42; i++) {
// calendar.push({
// date: formatDate(new Date(nextYear, nextMonth, i)),
// dayOfWeek: (lastDayInfo.dayOfWeek + i - 1) % 7,
// })
// }
return calendar;
};
exports.createMonthCalendarWithAdjacentMonths = createMonthCalendarWithAdjacentMonths;
const getMonthName = (month) => {
let currentMonth;
switch (month) {
case 0:
currentMonth = 'Tháng 1';
break;
case 1:
currentMonth = 'Tháng 2';
break;
case 2:
currentMonth = 'Tháng 3';
break;
case 3:
currentMonth = 'Tháng 4';
break;
case 4:
currentMonth = 'Tháng 5';
break;
case 5:
currentMonth = 'Tháng 6';
break;
case 6:
currentMonth = 'Tháng 7';
break;
case 7:
currentMonth = 'Tháng 8';
break;
case 8:
currentMonth = 'Tháng 9';
break;
case 9:
currentMonth = 'Tháng 10';
break;
case 10:
currentMonth = 'Tháng 11';
break;
case 11:
currentMonth = 'Tháng 12';
break;
default:
break;
}
return currentMonth;
};
exports.getMonthName = getMonthName;
const dateStrToDate = (date) => {
const fullDate = date === null || date === void 0 ? void 0 : date.split('-');
return new Date(parseInt(fullDate[2]), parseInt(fullDate[1]) - 1, parseInt(fullDate[0]));
};
exports.dateStrToDate = dateStrToDate;
const getHour = () => {
const currentHour = new Date().getHours();
const hourInDay = [];
for (let i = 0; i < 24; i++) {
i < 10 ? hourInDay.push(`0${i}`) : hourInDay.push(`${i}`);
}
const arrayHour = hourInDay.map((item) => {
return { name: item, current: parseInt(item) === currentHour };
});
return arrayHour;
};
exports.getHour = getHour;
const getMinutes = () => {
const currentMinute = new Date().getMinutes();
const minutes = [];
for (let i = 0; i < 60; i++) {
i < 10 ? minutes.push(`0${i}`) : minutes.push(`${i}`);
}
const arrayMinutes = minutes.map((item) => {
return { name: item, current: parseInt(item) === currentMinute };
});
return arrayMinutes;
};
exports.getMinutes = getMinutes;
const checkActive = (select, date) => {
const dateArr = date.split('-');
const selectArr = select.split('-');
return (parseInt(dateArr[0]) === parseInt(selectArr[0]) &&
parseInt(dateArr[1]) === parseInt(selectArr[1]) &&
parseInt(dateArr[2]) === parseInt(selectArr[2]));
};
exports.checkActive = checkActive;
const checkDisabled = (allDate) => {
if ((allDate === null || allDate === void 0 ? void 0 : allDate.startDisabled) && (allDate === null || allDate === void 0 ? void 0 : allDate.endDisabled)) {
return ((0, exports.dateStrToDate)(allDate.date) >= (0, exports.dateStrToDate)(allDate.startDisabled) &&
(0, exports.dateStrToDate)(allDate.date) <= (0, exports.dateStrToDate)(allDate.endDisabled));
}
if (allDate === null || allDate === void 0 ? void 0 : allDate.disabledDate) {
return allDate.disabledDate.includes(allDate.date);
}
if ((allDate === null || allDate === void 0 ? void 0 : allDate.max) || (allDate === null || allDate === void 0 ? void 0 : allDate.min)) {
return ((0, exports.dateStrToDate)(allDate.date) > (0, exports.dateStrToDate)(allDate.max) ||
(0, exports.dateStrToDate)(allDate.date) < (0, exports.dateStrToDate)(allDate.min));
}
};
exports.checkDisabled = checkDisabled;
//# sourceMappingURL=utils.js.map