UNPKG

@shinyongjun/react-datepicker

Version:
172 lines 6.06 kB
import { setMonthPage } from './page'; import { addLeadingZero } from './string'; export var checkHoliday = function (formatedDate, holidays) { var regex1 = /^\d{4}-\d{2}-\d{2}$/; var regex2 = /^\d{2}-\d{2}$/; return holidays.some(function (holiday) { return ((regex1.test(holiday) && formatedDate === holiday) || (regex2.test(holiday) && formatedDate.endsWith(holiday))); }); }; export var toLocalISOString = function (date) { var year = date.getFullYear(); var month = (date.getMonth() + 1).toString().padStart(2, '0'); var day = date.getDate().toString().padStart(2, '0'); var hours = date.getHours().toString().padStart(2, '0'); var minutes = date.getMinutes().toString().padStart(2, '0'); var seconds = date.getSeconds().toString().padStart(2, '0'); var milliseconds = date.getMilliseconds().toString().padStart(3, '0'); return "".concat(year, "-").concat(month, "-").concat(day, "T").concat(hours, ":").concat(minutes, ":").concat(seconds, ".").concat(milliseconds); }; export var formatDate = function (dateObj, format) { if (!dateObj) return format; var result = format; var localDate = toLocalISOString(dateObj); var _a = localDate.split('T'), dateStr = _a[0], timeStr = _a[1]; var _b = dateStr.split('-'), year = _b[0], month = _b[1], date = _b[2]; var removeMS = timeStr.split('.')[0]; var _c = removeMS.split(':'), hour = _c[0], minute = _c[1], second = _c[2]; var dayOfWeek = dateObj.getDay(); // 0 (일요일) ~ 6 (토요일) var dayNames = ['일', '월', '화', '수', '목', '금', '토']; if (/.*YYYY.*/.test(format)) result = result.replace(/YYYY/g, String(year)); if (/.*MM.*/.test(format)) result = result.replace(/MM/g, String(month)); if (/.*DD.*/.test(format)) result = result.replace(/DD/g, String(date)); if (/.*hh.*/.test(format)) result = result.replace(/hh/g, String(hour)); if (/.*mm.*/.test(format)) result = result.replace(/mm/g, String(minute)); if (/.*ss.*/.test(format)) result = result.replace(/ss/g, String(second)); if (/.*ddd.*/.test(format)) result = result.replace(/ddd/g, dayNames[dayOfWeek]); return result; }; export var formatDateValue = function (dateValue, timeValue, format) { return formatDate(dateValue.year !== null && dateValue.month !== null && dateValue.date !== null ? new Date(-1, setMonthPage("".concat(dateValue.year + 2, "-").concat(dateValue.month)), Number(dateValue.date), timeValue.hour, timeValue.minute, timeValue.second) : null, format); }; export var getDateUnit = function (value, unitType) { if (value === null) return ''; switch (unitType) { case 'YYYY': { return "".concat(value.getFullYear()); } case 'MM': { return addLeadingZero(value.getMonth() + 1); } case 'DD': { return addLeadingZero(value.getDate()); } default: { return ''; } } }; export var getDateValueUnit = function (value, unitType) { switch (unitType) { case 'YYYY': return value.year !== null ? "".concat(value.year) : 'YYYY'; case 'MM': return value.month !== null ? addLeadingZero(Number(value.month) + 1) : 'MM'; case 'DD': return value.date !== null ? addLeadingZero(value.date) : 'DD'; default: return ''; } }; export var getTimeValueUnit = function (value, unit) { switch (unit) { case 'hh': return addLeadingZero(value.hour); case 'mm': return addLeadingZero(value.minute); case 'ss': return addLeadingZero(value.second); default: return ''; } }; export var formatLabel = function (label, format) { var year = label.split('-')[0]; var month = label.split('-')[1]; if (/.*YYYY.*/.test(format) && /.*MM.*/.test(format)) { return format.replace(/YYYY/g, String(year)).replace(/MM/g, String(month)); } return label; }; export var getMonthArray = function (year, month) { var daysInMonth = new Date(year, month, 0, 9, 0, 0).getDate(); var monthArray = []; for (var day = 1; day <= daysInMonth; day++) { var date = new Date(year, month - 1, day, 12, 0, 0); var formattedDate = formatDate(date, 'YYYY-MM-DD'); var dayValue = date.getDate(); var dayOfWeek = date.getDay(); monthArray.push({ ISO: formattedDate, date: dayValue, day: dayOfWeek, }); } return monthArray; }; export var setViewDateByType = function (origin, value, type) { var split = origin.split('-'); var valueNum = Number(value); if (type === 'year') { if (valueNum < 1) { split[0] = 1; } else { split[0] = valueNum; } } if (type === 'month') { if (valueNum === 0) { if (Number(split[0]) > 1) { split[0] = Number(split[0]) - 1; split[1] = 12; } } else if (valueNum === 13) { split[0] = Number(split[0]) + 1; split[1] = 1; } else { split[1] = valueNum; } split[1] = addLeadingZero(split[1]); } if (type === 'date') split[2] = addLeadingZero(valueNum); return split.join('-'); }; /** * most pupolar date format * YYYY-MM-DD - 국제 표준 * DD/MM/YYYY * DD-MM-YYYY * MM/DD/YYYY * MM-DD-YYYY * YYYY년 MM월 DD일 (한국 스타일) * DD Month YYYY (영국 스타일) - 04 September 2023 * Month DD, YYYY (미국 스타일) - September 04, 2023 */ export var valueToDateObj = function (date, time) { if (date === null) return null; var newDate = new Date(date); newDate.setHours(time.hour, time.minute, time.second, 0); return newDate; }; //# sourceMappingURL=datetime.js.map