如何计算自己出生那天是该年当中的第几天

如何计算自己出生那天是该年当中的第几天

如何计算自己出生那天是该年当中的第几天

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

9回答
七月的孩子 2018-06-12 16:08:15
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来计算

  • 你好,以1988年为例,1988不能整除400会满足第一个条件,按照平年计算,但是实际1988年是闰年哦,建议:平年闰年的;逻辑可以按照参考:需满足下列条件之一,即为闰年(两个条件同时存在,一个为真即可): (1)年份能整除4但是不能整除100 (2)年份能整除400 祝学习愉快~
    2018-06-20 19:17:10
  • 谢谢老师!
    2018-06-21 11:00:36
  • 老师 年份不能整除100,那 2000年可以整除 4 也可以整除 100,同时 它是闰年啊
    2018-06-27 22:10:24
ukina 2018-05-31 09:27:39

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+"天。");

  • ukina #1
    第二个else if条件里m改成i,我打错了,不好意思,这样写应该很容易看懂吧
    2018-05-31 09:31:52
  • JIANGYUHANG 回复 ukina #2
    你还漏写了一个11月哦
    2018-06-05 21:02:53
  • ukina 回复 JIANGYUHANG #3
    哈,我写的太马虎了,第二个条件10改成9,再加个i=11就对了
    2018-06-06 09:50:18
慕的地8168385 2018-05-27 22:24:29

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)+"天");

}


viTaemin_nanali 2018-05-21 15:16:32

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();

不考虑闰年,谢谢!

慕标8539343 2018-05-15 21:31:22

<!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>


贝熊熊晚安 2018-05-07 21:52:42

<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+"是闰年&nbsp;&nbsp;");

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+"不是闰年&nbsp;&nbsp;");

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>


贝熊熊晚安 2018-05-07 21:51:40

我写的有一个 不过很麻烦,你要看吗

正在想名字呢 2018-05-07 19:05:44

你可以参考一下这个:https://class.imooc.com/course/qadetail/50302

好帮手慕星星 2018-05-06 09:39:21

可以把一年(平年)之中月份的天数存入一个数组中,根据输入的月份做循环相加,如果输入的是5月份,那么就循环相加1-4月的,在加上输入的天数。最后判断输入的年份是闰年还是平年,是否大于2月份,如果大于2月份,就需要在平年总天数的基础上加1,不是小于2月份,就不需要加1了。自己试着动手写一写,祝学习愉快~~

  • 提问者 慕仙1056934 #1
    麻烦写一下代码谢谢
    2018-05-07 18:39:14
  • 好像现在还没学数组吧
    2018-05-09 16:45:11
  • 是的呢,但是前面学习了for循环,我们主要是利用for循环来操作,如果现在不太明白,可以等到学完数组之后再用这种方法。
    2018-05-09 17:03:01
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
前端小白入门系列课程
  • 参与学习           人
  • 提交作业       11218    份
  • 解答问题       36713    个

从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师