10. ¿Cuántos días quedan hasta el cumpleaños de JavaScript, y la edad se calcula según la fecha de nacimiento?

1. ¿Cuántos días faltan para el cumpleaños?

<! DOCTYPE html> 
<html lang = "en"> 

<head> 
    <meta charset = "UTF-8"> 
    <meta name = "viewport" content = "width = device-width, initial-scale = 1.0"> 
    < title> Document </ title> 
</ head> 

<body> 
    <script> // Dado el mes y la fecha del cumpleaños, calcule cuántos días han pasado la función de cumpleaños getDaysToBirthday (mes, día) {
             var nowTime = new Date ();
             var thisYear = nowTime.getFullYear ();
             // cumpleaños de este año var birthday = new Date (thisYear, month-1 , day);// cumpleaños de este año ha pasado, el número de días desde la fecha de nacimiento del próximo año SI (cumpleaños <
        
        
            

            
            nowTime) { 
                birthday.setFullYear (nowTime.getFullYear () + 1 ); 
            } 
            var timeDec = cumpleaños - nowTime;
            var days = timeDec / (24 * 60 * 60 * 1000 );
            volver Math.ceil (días); 
        } 
        getDaysToBirthday ( 11, 2 );
    </script> 
</body> 

</html>
index.html

 

2. Calcule la edad según la fecha de nacimiento:

<! DOCTYPE html> 
<html lang = "en"> 

<head> 
    <meta charset = "UTF-8"> 
    <meta name = "viewport" content = "width = device-width, initial-scale = 1.0"> 
    < title> Document </ title> 
</ head> 

<body> 
    <script> // Si la función de año bisiesto esLeap (year) {
             return (year% 4 === 0 && year% 100! == 0) || (year% 400 === 0 ); 
        } // Calcular la edad en función de la fecha de nacimiento función getAge (año, mes, día) {
             // Obtener la fecha actual var nowTime = new Date ();var age = nowTime.getFullYear () -year; // calcula aproximadamente la edad
        
        

        
        
            
            
            

            // Si el cumpleaños es el 29 de febrero y este año no es bisiesto, el cumpleaños de este año se calculará el día 28 
            if (mes === 2 && day === 29 &&! IsLeap (nowTime.getFullYear ())) { 
                day = 28 ; 
            } 

            // Obtenga el cumpleaños de este año 
            var nowBirthday = new Date (nowTime.getFullYear (), month-1 , day); 
            console.log (nowBirthday, nowTime); 
            if (nowBirthday> nowTime) { // si este año es Si no ha cumplido años, perderá un año de 
                edad-- ; 
            } 

            return age; 
        } 

        console.log (getAge ( 2000, 2, 29 ));
     </ script> 
</ body>

</html>
index.html

 

Supongo que te gusta

Origin www.cnblogs.com/lanshanxiao/p/12758708.html
Recomendado
Clasificación