UNPKG

phx-react

Version:

PHX REACT

156 lines 6.43 kB
"use strict"; exports.__esModule = true; exports.checkDisabled = exports.checkActive = exports.getMinutes = exports.getHour = exports.dateStrToDate = exports.getMonthName = exports.createMonthCalendarWithAdjacentMonths = exports.formatDate = void 0; var formatDate = function (date) { var year = date.getFullYear(); var month = String(date.getMonth() + 1).padStart(2, '0'); var day = String(date.getDate()).padStart(2, '0'); return "".concat(day, "-").concat(month, "-").concat(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 var createMonthCalendarWithAdjacentMonths = function (year, month) { var getFirstDayOfMonth = function (year, month) { var firstDay = new Date(year, month, 1); var dayOfWeek = firstDay.getDay(); // 0 (Chủ Nhật) đến 6 (Thứ 7) return { date: firstDay, dayOfWeek: dayOfWeek }; }; // Hàm để lấy số ngày trong tháng var getDaysInMonth = function (year, month) { return new Date(year, month + 1, 0).getDate(); }; var firstDayInfo = getFirstDayOfMonth(year, month); var daysInMonth = getDaysInMonth(year, month); var 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 (var 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; var getMonthName = function (month) { var 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; var dateStrToDate = function (date) { var 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; var getHour = function () { var currentHour = new Date().getHours(); var hourInDay = []; for (var i = 0; i < 24; i++) { i < 10 ? hourInDay.push("0".concat(i)) : hourInDay.push("".concat(i)); } var arrayHour = hourInDay.map(function (item) { return { name: item, current: parseInt(item) === currentHour }; }); return arrayHour; }; exports.getHour = getHour; var getMinutes = function () { var currentMinute = new Date().getMinutes(); var minutes = []; for (var i = 0; i < 60; i++) { i < 10 ? minutes.push("0".concat(i)) : minutes.push("".concat(i)); } var arrayMinutes = minutes.map(function (item) { return { name: item, current: parseInt(item) === currentMinute }; }); return arrayMinutes; }; exports.getMinutes = getMinutes; var checkActive = function (select, date) { var dateArr = date.split('-'); var 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; var checkDisabled = function (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