在线Cron表达式解析器

在线Cron表达式解析器

功能1:输入6位数Cron表达式,输出10个最近会触发运行的具体日期



<h2>功能2:勾选具体运行选项,输出对应的6位数Cron表达式</h2>
<form>
	<label for="seconds">秒:</label>
	<input type="checkbox" id="seconds" name="seconds" value="*"><br>
	<label for="minutes">分:</label>
	<input type="checkbox" id="minutes" name="minutes" value="*"><br>
	<label for="hours">时:</label>
	<input type="checkbox" id="hours" name="hours" value="*"><br>
	<label for="days">天:</label>
	<input type="checkbox" id="days" name="days" value="*"><br>
	<label for="months">月:</label>
	<input type="checkbox" id="months" name="months" value="*"><br>
	<label for="weeks">周:</label>
	<input type="checkbox" id="weeks" name="weeks" value="*"><br><br>
	<button onclick="getCron()">获取Cron表达式</button>
</form>
<div id="cron"></div>

<script>
	function getDates() {
		const input = document.getElementById("corn").value;
		const cron = input.split(' ');
		const seconds = cron[0];
		const minutes = cron[1];
		const hours = cron[2];
		const days = cron[3];
		const months = cron[4];
		const weeks = cron[5];

		const now = new Date();
		const year = now.getFullYear();

		const dates = [];
		for (let i = 0; i < 10; i++) {
			const date = new Date(year, parseInt(months) - 1, parseInt(days), parseInt(hours), parseInt(minutes), parseInt(seconds));
			date.setDate(date.getDate() + 7 * i);
			if (weeks !== '*') {
				while (date.getDay() !== parseInt(weeks)) {
					date.setDate(date.getDate() + 1);
				}
			}
			if (date < now) {
				date.setDate(date.getDate() + 7);
			}
			dates.push(date);
		}

		const output = document.getElementById("dates");
		output.innerHTML = '';
		for (const date of dates) {
			output.innerHTML += date.toLocaleString() + '<br>';
		}
	}

	function getCron() {
		const seconds = document.getElementById("seconds").checked ? '*' : '0';
		const minutes = document.getElementById("minutes").checked ? '*' : '0';
		const hours = document.getElementById("hours").checked ? '*' : '0';
		const days = document.getElementById("days").checked ? '*' : '1';
		const months = document.getElementById("months").checked ? '*' : '1';
		const weeks = document.getElementById("weeks").checked ? '*' : '0';

		const cron = `${seconds} ${minutes} ${hours} ${days} ${months} ${weeks}`;

		const output = document.getElementById("cron");
		output.innerHTML = `Cron表达式:${cron}`;
	}
</script>
在线Cron表达式解析器 - 生成和解析Cron任务调度

原文地址: https://www.cveoy.top/t/topic/mQY4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录