欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于HTML+JS實現(xiàn)簡單的年齡計算器

 更新時間:2021年12月07日 09:44:28   作者:海擁?  
JavaScript提供了一些內(nèi)置的日期和時間函數(shù),有助于從日期(出生日期)開始計算年齡。本文主要介紹了使用這些JavaScript方法,制作一個簡單的年齡計算器,快來跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧

前言

在線演示地址:http://haiyong.site/age-calculator

JavaScript提供了一些內(nèi)置的日期和時間函數(shù),有助于從日期(出生日期)開始計算年齡。使用這些JavaScript方法,您可以輕松找到任何人的年齡。為此,我們需要用戶輸入日期和當前系統(tǒng)日期。

演示效果

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?

以上就是基于HTML+JS實現(xiàn)簡單的年齡計算器的詳細內(nèi)容,更多關(guān)于HTML JS 年齡計算器的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 淺析JavaScript訪問對象屬性和方法及區(qū)別

    淺析JavaScript訪問對象屬性和方法及區(qū)別

    這篇文章主要介紹了淺析JavaScript訪問對象屬性和方法及區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • 在JavaScript中獲取請求的URL參數(shù)[正則]

    在JavaScript中獲取請求的URL參數(shù)[正則]

    在ASP.NET后臺代碼中,對于這樣的URL請求地址:http://www.abc.com?id=001,我們可以通過Request.QueryString["id"]的方法很容易的獲取到URL中請求的參數(shù)的值,但是要在前臺js代碼中獲取請求的參數(shù)的值,應(yīng)該怎么做呢?
    2010-12-12
  • JavaScript數(shù)據(jù)結(jié)構(gòu)之棧實例用法

    JavaScript數(shù)據(jù)結(jié)構(gòu)之棧實例用法

    在本篇文章里小編給大家分享了關(guān)于JavaScript數(shù)據(jù)結(jié)構(gòu)之棧實例用法內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2019-01-01
  • js實現(xiàn)圖片切換(動畫版)

    js實現(xiàn)圖片切換(動畫版)

    本文主要對javascript實現(xiàn)圖片切換(動畫版)的方法進行步驟分析、實例介紹,具有很好的參考價值,需要的朋友一起來看下吧
    2016-12-12
  • js鏈表操作(實例講解)

    js鏈表操作(實例講解)

    下面小編就為大家?guī)硪黄猨s鏈表操作(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • JavaScript實現(xiàn)DOM對象選擇器

    JavaScript實現(xiàn)DOM對象選擇器

    這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)DOM對象選擇器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • JS編程小常識很有用

    JS編程小常識很有用

    JS是一門計算機編程語言,是一門動態(tài)語言也稱為腳本語言,是解析型編程語言。需要了解跟多
    2012-11-11
  • layer.confirm取消按鈕綁定事件的方法

    layer.confirm取消按鈕綁定事件的方法

    今天小編就為大家分享一篇layer.confirm取消按鈕綁定事件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 原生JS實現(xiàn)記憶翻牌游戲

    原生JS實現(xiàn)記憶翻牌游戲

    這篇文章主要為大家詳細介紹了原生JS實現(xiàn)記憶翻牌游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • JavaScript從數(shù)組中刪除指定值元素的方法

    JavaScript從數(shù)組中刪除指定值元素的方法

    這篇文章主要介紹了JavaScript從數(shù)組中刪除指定值元素的方法,實例分析了兩種常用的javascript操作數(shù)組指定元素的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03

最新評論