当前位置:网站首页>JS ask for the day of the year

JS ask for the day of the year

2022-06-13 08:34:00 Just one time

The user enters the date of the year to calculate the day of the year

Ideas
1. For the year , You need to judge whether the entered year is a leap year , If it is a leap year and the month is greater than 2 Monthly rule Add one to the total number of days
2. For months , For example, user input 4 month Then the current number of days should be before 3 Add the months and days of the month and then add the entered date .( You can store the days of each month in an array ) Traverse the array and accumulate the number of days .
3. For Japan , If user input 1 month , Then enter the number of days , It is the day of the year .

    <script>
        /*  Ideas : 1 First judge whether the year is a normal year or a leap year  ( In ordinary years 2 Monthly 28 God   Leap year's 2 Monthly 29 God ) 2 The number of days in each month in a normal year monthDay = [31,28,31,30,31,30,31,31,30,31,30,31]  If user input 1 month   The number of days is returned directly   If the year entered by the user is a leap year and the month entered is greater than 2  Then the total number of days plus 1  If user input  3 month  30 Japan   be   Traversal array  monthDay  The judgment condition is  i < month - 1 num = day + monthDay[i] */
        var year  = Number(prompt(' Please enter the current year '));
        var month = Number(prompt(' Please enter ' + year + ' The month in '));
        var day   = Number(prompt(' Please enter ' + year + ' year ' + month + ' The date of the month '));

        // Determine whether the year is a leap year 
        function isfullyear(year) {
    
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
    
                return true;
            }
        }
        alert(' At present, it is '+ year + ' In the middle of the year ' + getDays(year,month,day) + ' God ');


        // Defined function   Put the user input   year , month , Japan   Pass as an argument to a function 
        function getDays(year, month, day) {
    
            // Assign days to num
            var num = day;
            // If user input 1 month   The number of days is returned directly 
            if (month == 1) {
    
                return day;
            }

            var monthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

            // Cycle through the months   The cycle condition is the month before the user enters the month  
            for (var i = 0; i < month - 1; i++) {
    
                //num =  Days of the month  +  The number of days in the previous month 
                num = num + monthDay[i]
            }

            // If the year entered by the user is a leap year and the month entered is greater than 2  Then the total number of days plus 1
            if (isfullyear(year) && month > 2) {
    
                num++;
            }
            return num;
        }


    </script>
原网站

版权声明
本文为[Just one time]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270540177276.html