帮我写一个可以直接运行的html他是corn解析器请用js实现功能1输入6位格式的corn表达式对应秒、分、时、天、月、周点击输出可以输出几个最近会触发运行cron的具体日期示例功能2通过勾选具体怎么运行的选项对应秒、分、时、天、月、周然后输出对应的6位格式的corn表达式
Cron表达式解析器
功能1:解析Cron表达式
请输入6位格式的Cron表达式(秒 分 时 日 月 周):
<div id="cronResult"></div>
<h2>功能2:生成Cron表达式</h2>
<p>请选择运行时间:</p>
<input type="checkbox" id="secondCheckbox" onchange="generateCronExpression()">秒
<input type="checkbox" id="minuteCheckbox" onchange="generateCronExpression()">分
<input type="checkbox" id="hourCheckbox" onchange="generateCronExpression()">时
<input type="checkbox" id="dayCheckbox" onchange="generateCronExpression()">日
<input type="checkbox" id="monthCheckbox" onchange="generateCronExpression()">月
<input type="checkbox" id="weekCheckbox" onchange="generateCronExpression()">周
<div id="generatedCronExpression"></div>
<script>
function parseCron() {
const cronExpression = document.getElementById("cronExpression").value;
const cronResult = document.getElementById("cronResult");
if (!cronExpression) {
cronResult.innerHTML = "<p>请输入Cron表达式</p>";
return;
}
const cronParts = cronExpression.split(" ");
if (cronParts.length !== 6) {
cronResult.innerHTML = "<p>请输入正确的Cron表达式</p>";
return;
}
const second = cronParts[0];
const minute = cronParts[1];
const hour = cronParts[2];
const day = cronParts[3];
const month = cronParts[4];
const week = cronParts[5];
const date1 = new Date();
const date2 = new Date();
const date3 = new Date();
const date4 = new Date();
const date5 = new Date();
const date6 = new Date();
date1.setSeconds(date1.getSeconds() + getNext(second));
date2.setMinutes(date2.getMinutes() + getNext(minute));
date3.setHours(date3.getHours() + getNext(hour));
date4.setDate(date4.getDate() + getNext(day));
date5.setMonth(date5.getMonth() + getNext(month));
date6.setDate(date6.getDate() + getNextWeekday(week));
cronResult.innerHTML = `
<p>下一次运行时间:</p>
<p>${date1}</p>
<p>${date2}</p>
<p>${date3}</p>
<p>${date4}</p>
<p>${date5}</p>
<p>${date6}</p>
`;
}
function getNext(expression) {
const now = new Date();
const currentValue = parseInt(now.getSeconds());
const values = expression.split(",");
for (let i = 0; i < values.length; i++) {
const value = values[i];
if (value === "*") {
return 0;
}
if (value.includes("/")) {
const step = parseInt(value.split("/")[1]);
if (currentValue % step === 0) {
return 0;
}
}
if (value.includes("-")) {
const rangeValues = value.split("-");
const minValue = parseInt(rangeValues[0]);
const maxValue = parseInt(rangeValues[1]);
if (currentValue >= minValue && currentValue <= maxValue) {
return 0;
}
}
if (value === currentValue.toString()) {
return 0;
}
if (value === "?") {
return 0;
}
if (value === "L") {
return -1;
}
if (value.includes("W")) {
const dayOfMonth = parseInt(value.replace("W", ""));
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
const weekday = new Date(now.getFullYear(), now.getMonth(), dayOfMonth).getDay();
if (dayOfMonth < lastDayOfMonth && (weekday === 0 || weekday === 6)) {
return 0;
} else if (dayOfMonth === lastDayOfMonth && weekday === 6) {
return -2;
} else if (dayOfMonth === lastDayOfMonth && weekday !== 6) {
return -1;
} else {
const nextWeekday = getNextWeekday("1-5");
const nextDate = new Date(now.getFullYear(), now.getMonth(), dayOfMonth + nextWeekday);
return nextDate.getSeconds() - now.getSeconds();
}
}
if (value.includes("#")) {
const dayOfWeek = value.split("#")[0];
const occurrence = parseInt(value.split("#")[1]);
const currentWeekday = now.getDay();
let diff;
if (currentWeekday > dayOfWeek) {
diff = 7 - currentWeekday + dayOfWeek;
} else if (currentWeekday < dayOfWeek) {
diff = dayOfWeek - currentWeekday;
} else {
if (now.getHours() === 0 && now.getMinutes() === 0 && now.getSeconds() < occurrence * 60) {
return occurrence * 60 - now.getSeconds();
} else {
diff = 0;
}
}
const nextDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + diff);
return (occurrence - 1) * 7 * 24 * 60 * 60 + nextDate.getSeconds() - now.getSeconds();
}
}
return -1;
}
function getNextWeekday(expression) {
const now = new Date();
const currentValue = now.getDay();
const values = expression.split(",");
for (let i = 0; i < values.length; i++) {
const value = values[i];
if (value === "*") {
return 0;
}
if (value.includes("/")) {
const step = parseInt(value.split("/")[1]);
if (currentValue % step === 0) {
return 0;
}
}
if (value.includes("-")) {
const rangeValues = value.split("-");
const minValue = parseInt(rangeValues[0]);
const maxValue = parseInt(rangeValues[1]);
if (currentValue >= minValue && currentValue <= maxValue) {
return 0;
}
}
if (value === currentValue.toString()) {
return 0;
}
if (value === "?") {
return 0;
}
if (value === "L") {
return -1;
}
if (value === "1") {
return currentValue === 0 ? 1 : 8 - currentValue;
}
if (value === "2") {
return currentValue === 0 ? 2 : 9 - currentValue;
}
if (value === "3") {
return currentValue === 0 ? 3 : 10 - currentValue;
}
if (value === "4") {
return currentValue === 0 ? 4 : 11 - currentValue;
}
if (value === "5") {
return currentValue === 0 ? 5 : 12 - currentValue;
}
if (value === "6") {
return currentValue === 0 ? 6 : 13 - currentValue;
}
if (value === "7") {
return currentValue === 0 ? 7 : 14 - currentValue;
}
if (value.includes("W")) {
const dayOfMonth = parseInt(value.replace("W", ""));
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
const weekday = new Date(now.getFullYear(), now.getMonth(), dayOfMonth).getDay();
if (dayOfMonth < lastDayOfMonth && (weekday === 0 || weekday === 6)) {
return 1;
} else if (dayOfMonth === lastDayOfMonth && weekday === 6) {
return -1;
} else if (dayOfMonth === lastDayOfMonth && weekday !== 6) {
return -2;
} else {
return weekday - currentValue >= 0 ? weekday - currentValue : 7 - currentValue + weekday;
}
}
}
return -1;
}
function generateCronExpression() {
const secondCheckbox = document.getElementById("secondCheckbox");
const minuteCheckbox = document.getElementById("minuteCheckbox");
const hourCheckbox = document.getElementById("hourCheckbox");
const dayCheckbox = document.getElementById("dayCheckbox");
const monthCheckbox = document.getElementById("monthCheckbox");
const weekCheckbox = document.getElementById("weekCheckbox");
let cronExpression = "";
cronExpression += secondCheckbox.checked ? "*" : "0";
cronExpression += " ";
cronExpression += minuteCheckbox.checked ? "*" : "0";
cronExpression += " ";
cronExpression += hourCheckbox.checked ? "*" : "0";
cronExpression += " ";
cronExpression += dayCheckbox.checked ? "*" : "?";
cronExpression += " ";
cronExpression += monthCheckbox.checked ? "*" : "1";
cronExpression += " ";
cronExpression += weekCheckbox.checked ? "?" : "*";
const generatedCronExpression = document.getElementById("generatedCronExpression");
generatedCronExpression.innerHTML = `
<p>生成的Cron表达式:</p>
<p>${cronExpression}</p>
`;
}
</script>
原文地址: http://www.cveoy.top/t/topic/bpc2 著作权归作者所有。请勿转载和采集!