帮我写一个可以直接运行的html他是corn解析器请用js实现功能1输入6位格式的corn表达式对应秒、分、时、天、月、周点击输出可以输出几个最近会触发运行cron的具体日期格式类似2022-01-02 120201功能2通过勾选具体怎么运行的选项对应秒、分、时、天、月、周然后输出对应的6位格式的corn表达式
<!DOCTYPE html>
<html>
<head>
<title>Cron解析器</title>
</head>
<body>
<h1>Cron解析器</h1>
<h2>功能1:解析corn表达式</h2>
<label for="cronExpression">请输入6位格式的corn表达式:</label>
<input type="text" id="cronExpression" placeholder="例如:0 0 12 * * *">
<br><br>
<button onclick="parseCronExpression()">输出</button>
<br><br>
<p id="output"></p>
<h2>功能2:生成corn表达式</h2>
<label>请选择怎么运行的选项:</label>
<br><br>
<input type="checkbox" id="second" onchange="generateCronExpression()">秒
<input type="checkbox" id="minute" onchange="generateCronExpression()">分
<input type="checkbox" id="hour" onchange="generateCronExpression()">小时
<input type="checkbox" id="day" onchange="generateCronExpression()">天
<input type="checkbox" id="month" onchange="generateCronExpression()">月
<input type="checkbox" id="week" onchange="generateCronExpression()">周
<br><br>
<p id="cronExpressionOutput"></p>
<script>
function parseCronExpression() {
let cronExpression = document.getElementById("cronExpression").value;
let output = document.getElementById("output");
let nextDates = getNextDates(cronExpression);
if (nextDates.length > 0) {
let outputString = "下次运行时间:";
for (let i = 0; i < nextDates.length; i++) {
outputString += nextDates[i].toLocaleString() + ", ";
}
outputString = outputString.slice(0, -2) + ".";
output.textContent = outputString;
} else {
output.textContent = "无效的corn表达式。";
}
}
<pre><code> function getNextDates(cronExpression) {
let fields = cronExpression.split(" ");
if (fields.length !== 6) {
return [];
}
let currentDate = new Date();
let nextDates = [];
let second = fields[0];
let minute = fields[1];
let hour = fields[2];
let dayOfMonth = fields[3];
let month = fields[4];
let dayOfWeek = fields[5];
let cronDate = new Date(currentDate.getFullYear(), parseInt(month) - 1, parseInt(dayOfMonth), parseInt(hour), parseInt(minute), parseInt(second));
if (cronDate < currentDate) {
cronDate.setFullYear(cronDate.getFullYear() + 1);
}
while (cronDate <= currentDate) {
switch (dayOfWeek) {
case "*":
break;
case "0":
if (cronDate.getDay() !== 0) {
cronDate.setDate(cronDate.getDate() + 7 - cronDate.getDay());
}
break;
case "1":
if (cronDate.getDay() !== 1) {
cronDate.setDate(cronDate.getDate() + (cronDate.getDay() === 0 ? 1 : 8 - cronDate.getDay()));
}
break;
case "2":
if (cronDate.getDay() !== 2) {
cronDate.setDate(cronDate.getDate() + (cronDate.getDay() <= 2 ? 2 - cronDate.getDay() : 9 - cronDate.getDay()));
}
break;
case "3":
if (cronDate.getDay() !== 3) {
cronDate.setDate(cronDate.getDate() + (cronDate.getDay() <= 3 ? 3 - cronDate.getDay() : 10 - cronDate.getDay()));
}
break;
case "4":
if (cronDate.getDay() !== 4) {
cronDate.setDate(cronDate.getDate() + (cronDate.getDay() <= 4 ? 4 - cronDate.getDay() : 11 - cronDate.getDay()));
}
break;
case "5":
if (cronDate.getDay() !== 5) {
cronDate.setDate(cronDate.getDate() + (cronDate.getDay() <= 5 ? 5 - cronDate.getDay() : 12 - cronDate.getDay()));
}
break;
case "6":
if (cronDate.getDay() !== 6) {
cronDate.setDate(cronDate.getDate() + (cronDate.getDay() <= 6 ? 6 - cronDate.getDay() : 13 - cronDate.getDay()));
}
break;
}
if (cronDate <= currentDate) {
nextDates.push(new Date(cronDate.getTime()));
}
switch (month) {
case "*":
cronDate.setMonth(cronDate.getMonth() + 1);
break;
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "10":
case "11":
if (cronDate.getMonth() !== parseInt(month) - 1) {
cronDate.setMonth(parseInt(month) - 1);
} else {
cronDate.setMonth(parseInt(month));
}
break;
case "12":
if (cronDate.getMonth() !== 11) {
cronDate.setMonth(11);
} else {
cronDate.setFullYear(cronDate.getFullYear() + 1);
cronDate.setMonth(0);
}
break;
}
if (cronDate <= currentDate) {
switch (dayOfMonth) {
case "*":
cronDate.setDate(1);
cronDate.setMonth(cronDate.getMonth() + 1);
break;
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "10":
case "11":
case "12":
case "13":
case "14":
case "15":
case "16":
case "17":
case "18":
case "19":
case "20":
case "21":
case "22":
case "23":
case "24":
case "25":
case "26":
case "27":
case "28":
case "29":
case "30":
if (cronDate.getDate() !== parseInt(dayOfMonth)) {
cronDate.setDate(parseInt(dayOfMonth));
} else {
cronDate.setMonth(cronDate.getMonth() + 1);
}
break;
case "31":
if (cronDate.getDate() !== 31) {
cronDate.setDate(31);
} else {
cronDate.setMonth(cronDate.getMonth() + 1);
}
break;
}
}
}
return nextDates;
}
function generateCronExpression() {
let cronExpressionOutput = document.getElementById("cronExpressionOutput");
let second = document.getElementById("second").checked ? "*" : "0";
let minute = document.getElementById("minute").checked ? "*" : "0";
let hour = document.getElementById("hour").checked ? "*" : "0";
let day = document.getElementById("day").checked ? "*" : "1";
let month = document.getElementById("month").checked ? "*" : "1";
let week = document.getElementById("week").checked ? "*" : "?";
let cronExpression = second + " " + minute + " " + hour + " " + day + " " + month + " " + week;
cronExpressionOutput.textContent = "生成的corn表达式:" + cronExpression;
}
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/bpfa 著作权归作者所有。请勿转载和采集!