正在回答 回答被采纳积分+1
var year,mon,day; function days(){ // 检测输入的出生日期是否符合要求 while(year!==false){ year=(prompt("请输入您的出生年份")); if(year==null){ return; }else if(year.toString().length!=4||isNaN(parseInt(year))){ alert("必须是4位数字"); }else{ break; } } while(mon!==false){ mon=parseInt(prompt("出生月份")); if(isNaN(mon)||mon.toString().length>2||mon>12){ alert("不超过两位的数字且小于12"); }else{ break; } } while(day!==false){ day=parseInt(prompt("出生日")); if(isNaN(day)||day.toString().length>2||day>31){ alert("不超过两位的数字且小于31"); }else{ break; } } // 计算平年出生在哪一天 var sum=0; if(year%4!=0 && year%100==0 || year%400!=0){ if(mon!=1){ for(i=0;i<mon-1;i++){ sum+=arguments[i]; } var s=sum+day; document.write("您的生日是"+year+"年第"+s+"天"); }else{ document.write("您的生日是"+year+"年第"+day+"天"); } }else{ // 计算闰年出生在哪一天 var num=0; for(f=0;f<mon-1;f++){ num=num+arguments[f]; } // 当用户输入的日期小于2月29时,不+1 if(mon<=2&&day<=29){ var h=day+num document.write("您的生日是"+year+"年第"+h+"天"); // 当用户输入的日期大于2月29时+1 }else{ var h=(day+1)+num document.write("您的生日是"+year+"年第"+h+"天"); } } } days(31,28,31,30,31,30,31,31,30,31,30,31);
这是我的完整作业,你可以拿去参考一下。
至于怎么计算出生在第几天,可以使用之前练习求平均数的方法,使用for循环搭配arguments来计算
var y=parseInt(prompt("请输入您的出生年份")),
m=parseInt(prompt("请输入您的出生月份")),
d=parseInt(prompt("请输入您的出生日期")),
days=0;
for(var i=0;i<m;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
days+=31;
}else if(i==4||m==6||m==10){
days+=30;
}else if(i==2){
if(y%400==0||(y%4==0&&y%100!==0)){
days+=29;
}else days+=28;
}
}
days=days+d;
document.write("您的生日在"+y+"年是第"+days+"天。");
function isYear(year){
return year%400?year%4?false:year%100?true:false:true;
}
function monthDays(year,month){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(isYear(year)){
return 29;
}else{
return 28;
}
default:
return 0;
}
}
function totalDays(year,month,days){
var nDays=0;
for(var i=1;i<month;i++){
nDays+=monthDays(year,i);
}
return nDays+parseInt(days);
}
function mainPro(){
var year = prompt("请输入您的出生年份");
var month = prompt("请输入您的出生月份");
var day = prompt("请输入您的出生日期");
document.write("您的生日在"+year+"第"+totalDays(year,month,day)+"天");
}
function getDays(){
var getYear=prompt("请输入您的出生年"),
getMonth=prompt("请输入您的出生月"),
getDay=prompt("请输入您的出生日");
var birthDate=new Date();
birthDate.setYear(parseInt(getYear));
birthDate.setMonth(parseInt(getMonth-1));
birthDate.setDate(parseInt(getDay));
var birthTime=birthDate.getTime();
var thisYear=new Date(parseInt(getYear),0,1),
thisTime=thisYear.getTime();
var days=Math.ceil((birthTime-thisTime)/(24*60*60*1000));
document.write("您生日是"+birthDate.getYear()+"年"+(birthDate.getMonth()+1)+"月"+birthDate.getDate()+"日,是该年的第"+days+"天");
}
getDays();
不考虑闰年,谢谢!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>生日是第几天</title>
</head>
<body>
<script type="text/javascript">
var year=prompt("请输入您的出生年份");
var month=prompt("请输入您的出生月份");
var date=prompt("请输入您的出生日期");
function birth(year,month,date){
var n;
if(year%4==0){
switch(month){
case "1":
n=Number(date);
break;
case "2":
n=Number(date)+31;
break;
case "3":
n=Number(date)+60;
break;
case "4":
n=Number(date)+91;
break;
case "5":
n=Number(date)+121;
break;
case "6":
n=Number(date)+152;
break;
case "7":
n=Number(date)+182;
break;
case "8":
n=Number(date)+213;
break;
case "9":
n=Number(date)+244;
break;
case "10":
n=Number(date)+274;
break;
case "11":
n=Number(date)+305;
break;
case "12":
n=Number(date)+335;
break;
}
}else{
switch(month){
case "1":
n=Number(date);
break;
case "2":
n=Number(date)+31;
break;
case "3":
n=Number(date)+59;
break;
case "4":
n=Number(date)+90;
break;
case "5":
n=Number(date)+120;
break;
case "6":
n=Number(date)+151;
break;
case "7":
n=Number(date)+181;
break;
case "8":
n=Number(date)+212;
break;
case "9":
n=Number(date)+243;
break;
case "10":
n=Number(date)+273;
break;
case "11":
n=Number(date)+304;
break;
case "12":
n=Number(date)+334;
break;
}
}
return "您的生日在"+year+"年是第"+n+"天";
}
document.write(birth(year,month,date));
</script>
</body>
</html>
<script type="text/javascript">
var year = prompt("请输入年份");
var month = prompt("请输入月份");
var day = prompt("请输入出生日期");
var Global = parseInt(day);
function func3(day_1,day2,day_2,day3,day4,day5,day6,day7,day8,day9,day10,day11,day12,year001,month001,day001){
var temp = 0;
var temp1 = 1;
for(var i = 0;i < month;i++){
if(year001 % 400 == 0 || (year001 % 4 == 0 && year001 % 100 != 0)){
//31,28,29,31,30,31,30,31,31,30,31,30,31
if(i == 1 ){
temp += arguments[i+1];
continue;
}
if(temp1){
temp += arguments[i];
temp1--;
}else{
temp += arguments[i+1];
}
}else{
if(i == 1 ){
temp += arguments[i];
continue;
}
if(temp1){
temp += arguments[i];
temp1--;
}else{
temp += arguments[i+1];
}
}
}
return temp;
}
function func1(year1,month1,day1){
if(year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)){
document.write(year1+"是闰年 ");
switch(month1){
case "1":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "2":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-29)+Global);
break;
case "3":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "4":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-30)+Global);
break;
case "5":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "6":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-30)+Global);
break;
case "7":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "8":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "9":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-30)+Global);
break;
case "10":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "11":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-30)+Global);
break;
case "12":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
}
}else{
document.write(year1+"不是闰年 ");
switch(month1){
case "1":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "2":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-28)+Global);
break;
case "3":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "4":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-30)+Global);
break;
case "5":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "6":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-30)+Global);
break;
case "7":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "8":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "9":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-30)+Global);
break;
case "10":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
case "11":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-30)+Global);
break;
case "12":
func2(year1,(func3(31,28,29,31,30,31,30,31,31,30,31,30,31,year1,month1,day1)-31)+Global);
break;
}
}
}
function func2(year2,num2){
document.write("您的生日在"+year2+"年,第"+num2+"天");
}
func1(year,month,day);
</script>
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36713 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星