@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
75 lines (74 loc) • 1.77 kB
JavaScript
const isLeapYear = (y) => {
return y % 4 === 0 && y % 100 !== 0 || y % 400 === 0;
};
const getWhatDay = (year, month, day) => {
const date = new Date(year, month - 1, day);
const dayNames = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
];
return dayNames[date.getDay()];
};
const getMonthPreDay = (year, month) => {
const day = new Date(year, month - 1, 1).getDay();
return day === 0 ? 7 : day;
};
const getMonthDays = (year, month) => {
if (/^0/.test(month)) {
month = month.split("")[1];
}
return [
0,
31,
isLeapYear(Number(year)) ? 29 : 28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
][month];
};
const getNumTwoBit = (n) => {
return n > 9 ? `${n}` : `0${n}`;
};
const date2Str = (date, split = "-") => {
const y = date.getFullYear();
const m = getNumTwoBit(date.getMonth() + 1);
const d = getNumTwoBit(date.getDate());
return [y, m, d].join(split);
};
const getDateString = (offset = 0) => {
const date = /* @__PURE__ */ new Date();
date.setDate(date.getDate() + offset);
return date2Str(date, "-");
};
const compareDate = (date1, date2) => {
const startTime = new Date(date1.replace("-", "/").replace("-", "/"));
const endTime = new Date(date2.replace("-", "/").replace("-", "/"));
return startTime < endTime;
};
const isEqual = (date1, date2) => {
const startTime = new Date((date1 || "").replace(/-/g, "/")).getTime();
const endTime = new Date(date2.replace(/-/g, "/")).getTime();
return startTime === endTime;
};
export {
getNumTwoBit as a,
getMonthDays as b,
getMonthPreDay as c,
date2Str as d,
getWhatDay as e,
compareDate as f,
getDateString as g,
isEqual as i
};