线上运行,可以直接打开:怀孕计算器(在线计算器)
作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class=”calculator”><label for=”lastPeriod”>最后一次月经的第一天:</label> <input id=”lastPeriod” type=”date”> <label for=”cycleLength”>月经周期长度 (天):</label> <input id=”cycleLength” type=”number” value=”28″ placeholder=”一般为28天”><button onclick=”calculatePregnancy()”>计算怀孕信息</button><div id=”result” class=”result”>结果将显示在这里</div></div>
JS:
function calculatePregnancy() {
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 = “请输入有效的日期和周期长度。”;
return;
}// 假设受孕日期为最后一次月经开始后的14天
const conceptionDate = new Date(lastPeriodDate);
conceptionDate.setDate(conceptionDate.getDate() + (cycleLength – 14));// 预产期为最后一次月经的280天(40周)之后
const dueDate = new Date(lastPeriodDate);
dueDate.setDate(dueDate.getDate() + 280);// 当前怀孕天数和周数
const today = new Date();
const daysPregnant = Math.floor((today – conceptionDate) / (1000 * 60 * 60 * 24));
const weeksPregnant = Math.floor(daysPregnant / 7);document.getElementById(‘result’).innerHTML =
`预产期:${dueDate.toLocaleDateString()}<br>` +
`受孕日期:${conceptionDate.toLocaleDateString()}<br>` +
`当前怀孕:${weeksPregnant} 周 (${daysPregnant} 天)`;
}
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;
}
暂无评论内容