基于HTML+JS实现简单的年龄计算器
作者:海拥? 时间:2024-04-23 09:27:29
前言
在线演示地址:http://haiyong.site/age-calculator
JavaScript提供了一些内置的日期和时间函数,有助于从日期(出生日期)开始计算年龄。使用这些JavaScript方法,您可以轻松找到任何人的年龄。为此,我们需要用户输入日期和当前系统日期。
演示效果
HTML代码
<div class="container">
<div class="inputs-wrapper">
<input type="date" id="date-input">
<button onclick="ageCalculate()">Calculate</button>
</div>
<div class="outputs-wrapper">
<div>
<span id="years">
-
</span>
<p>
Years
</p>
</div>
<div>
<span id="months">
-
</span>
<p>
Months
</p>
</div>
<div>
<span id="days">
-
</span>
<p>
Days
</p>
</div>
</div>
</div>
CSS代码
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
background-color: #ff6666;
}
.container{
width: 40%;
min-width: 450px;
position: absolute;
transform: translate(-50%,-50%);
left: 50%;
top: 50%;
padding: 50px 30px;
}
.container *{
font-family: "Poppins",sans-serif;
border: none;
outline: none;
}
.inputs-wrapper{
background-color: #080808;
padding: 30px 25px;
border-radius: 8px;
box-shadow: 0 15px 20px rgba(0,0,0,0.3);
margin-bottom: 50px;
}
input,
button{
height: 50px;
background-color: #ffffff;
color: #080808;
font-weight: 500;
border-radius: 5px;
}
input{
width: 60%;
padding: 0 20px;
font-size: 14px;
}
button{
width: 30%;
float: right;
}
.outputs-wrapper{
width: 100%;
display: flex;
justify-content: space-between;
}
.outputs-wrapper div{
height: 100px;
width: 100px;
background-color: #080808;
border-radius: 5px;
color: #ffffff;
display: grid;
place-items: center;
padding: 20px 0;
box-shadow: 0 15px 20px rgba(0,0,0,0.3);
}
span{
font-size: 30px;
font-weight: 500;
}
p{
font-size: 14px;
color: #707070;
font-weight: 400;
}
Javascript代码
const months = [31,28,31,30,31,30,31,31,30,31,30,31];
function ageCalculate(){
let today = new Date();
let inputDate = new Date(document.getElementById("date-input").value);
let birthMonth,birthDate,birthYear;
let birthDetails = {
date:inputDate.getDate(),
month:inputDate.getMonth()+1,
year:inputDate.getFullYear()
}
let currentYear = today.getFullYear();
let currentMonth = today.getMonth()+1;
let currentDate = today.getDate();
leapChecker(currentYear);
if(
birthDetails.year > currentYear ||
( birthDetails.month > currentMonth && birthDetails.year == currentYear) ||
(birthDetails.date > currentDate && birthDetails.month == currentMonth && birthDetails.year == currentYear)
){
alert("Not Born Yet");
displayResult("-","-","-");
return;
}
birthYear = currentYear - birthDetails.year;
if(currentMonth >= birthDetails.month){
birthMonth = currentMonth - birthDetails.month;
}
else{
birthYear--;
birthMonth = 12 + currentMonth - birthDetails.month;
}
if(currentDate >= birthDetails.date){
birthDate = currentDate - birthDetails.date;
}
else{
birthMonth--;
let days = months[currentMonth - 2];
birthDate = days + currentDate - birthDetails.date;
if(birthMonth < 0){
birthMonth = 11;
birthYear--;
}
}
displayResult(birthDate,birthMonth,birthYear);
}
function displayResult(bDate,bMonth,bYear){
document.getElementById("years").textContent = bYear;
document.getElementById("months").textContent = bMonth;
document.getElementById("days").textContent = bDate;
}
function leapChecker(year){
if(year % 4 == 0 || (year % 100 == 0 && year % 400 == 0)){
months[1] = 29;
}
else{
months[1] = 28;
}
}
演示地址
http://haiyong.site/age-calculator
来源:https://haiyong.blog.csdn.net/article/details/116721314
标签:HTML,JS,年龄,计算器
0
投稿
猜你喜欢
Pycharm运行加载文本出现错误的解决方法
2021-02-01 09:07:18
SQLServe 重复行删除方法
2024-01-26 18:39:24
PHP函数原理理解详谈
2023-06-04 01:56:59
使用PyInstaller库把Python程序打包成exe
2023-11-27 17:40:28
用SQL语句删除重复记录的四种方法
2011-05-03 09:25:00
精简版的MySQL制作步骤
2011-03-08 09:52:00
Python中ROS和OpenCV结合处理图像问题
2023-04-27 00:33:02
python 统计一个列表当中的每一个元素出现了多少次的方法
2021-09-21 13:48:32
python format 格式化输出方法
2023-12-24 16:26:19
python在地图上画比例的实例详解
2023-06-22 20:54:00
python self,cls,decorator的理解
2023-12-21 00:09:35
如何使用Python程序完成描述性统计分析需求
2021-01-11 22:11:20
解决numpy矩阵相减出现的负值自动转正值的问题
2021-10-07 19:49:16
Python中线程threading.Thread的使用详解
2023-07-22 13:25:48
pyftplib中文乱码问题解决方案
2023-11-10 11:34:07
python多进程执行方法apply_async使用说明
2023-01-31 11:56:10
Mysql使用索引实现查询优化
2024-01-16 03:59:35
Oracle 游标使用总结
2009-10-02 17:36:00
SQL Server查询条件IN中能否使用变量的示例详解
2024-01-15 17:55:55
go语言区块链学习调用智能合约
2024-04-30 10:01:34