根据一个六位数 数字日期,判断这个日期是这一年的第几天。(使用switch穿透编写)

var date =Number(prompt('请输入一个组成为年月日的八位数','如20160211'))

            var year =parseInt(date/10000);

            var month =parseInt(date/100)-year*100;

            var days =date-(year*10000 + month*100);

            var sum =0;

            switch(month){

                case 12:

                    sum+=30;

                case 11:

                    sum+=31;

                case 10:

                    sum+=30;

                case 9:

                    sum+=31;

                case 8:

                    sum+=31;

                case 7:

                    sum+=30;

                case 6:

                    sum+=31;

                case 5:

                    sum+=30;

                case 4:

                    sum+=31;

                case 3:

                    if(year%4==0 && year%100!=0 || year%400==0){

                        sum+=29

                    }else{

                        sum+=28

                    }

                case 2:

                    sum+=31;

                case 1:

                    sum+=days;

                    break;

                default:

                    alert('你输入的日期不符合规范')

            }

            alert(date  + '是' + year +'年第'+ sum +'天')

你可能感兴趣的:(根据一个六位数 数字日期,判断这个日期是这一年的第几天。(使用switch穿透编写))