您当前的位置: 首页 >  html5

jeff one

暂无认证

  • 0浏览

    0关注

    220博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

用HTML5+js 计算年龄

jeff one 发布时间:2021-12-23 13:38:58 ,浏览量:0

用HTML5+js 计算年龄

HTML5代码如下:

DOCTYPE html>

	
		
		
	
	
		
			
				
				Calculate
			
			
				
					
						-
					
					
						Years
					
				
				
					
						-
					
					
						Months
					
				
				
					
						-
					
					
						Days
					
				
			
		
		
			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             
关注
打赏
1661150981
查看更多评论
0.0408s