線上運(yùn)行,可以直接打開:經(jīng)期計(jì)算器(在線計(jì)算器)
? ? ? ?作為程序員,沒有合適的工具,就得手搓一個(gè),PC端,移動(dòng)端均可適用。廢話不多說,直接上代碼。
HTML:
<div class="calculator"><label for="lastPeriod">上次月經(jīng)日期:</label> <input id="lastPeriod" type="date" placeholder="選擇上次月經(jīng)日期"> <label for="cycleLength">經(jīng)期周期長度 (天):</label> <input id="cycleLength" type="number" placeholder="輸入經(jīng)期周期長度"><button onclick="calculatePeriod()">計(jì)算</button><div id="result" class="result">結(jié)果將顯示在這里</div></div>
JS:
function calculatePeriod() { const lastPeriodDate = new Date(document.getElementById('lastPeriod').value); const cycleLength = parseInt(document.getElementById('cycleLength').value); if (isNaN(lastPeriodDate.getTime()) || isNaN(cycleLength)) { document.getElementById('result').textContent = "請(qǐng)輸入有效的日期和周期長度。"; return; } // 預(yù)測下一次月經(jīng)的日期 const nextPeriodDate = new Date(lastPeriodDate); nextPeriodDate.setDate(lastPeriodDate.getDate() + cycleLength); // 預(yù)測排卵期(一般為周期的中間) const ovulationDate = new Date(lastPeriodDate); ovulationDate.setDate(lastPeriodDate.getDate() + cycleLength / 2); document.getElementById('result').innerHTML = `下一次月經(jīng)日期:${nextPeriodDate.toLocaleDateString()}<br>` + `預(yù)計(jì)排卵日期:${ovulationDate.toLocaleDateString()}`; }
?CSS:
.calculator { width: 100%; background-color: #333; color: white; padding: 20px; border-radius: 10px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); } label { display: block; margin-bottom: 10px; font-size: 16px; } input, select { width: 100%!important; padding: 10px!important; margin-bottom: 20px; color: #000000; border-radius: 5px; border: 1px solid #555; font-size: 16px!important; background-color: #ffffff!important; } button { width: 100%; padding: 10px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } button:hover { background-color: orange; } .result { font-size: 18px; margin-top: 20px; text-align: center; } option { background-color: #ffffff; } p { font-size: 18px; margin-top: 5px!important; }