js--输入一个日期,判断是那一年的第几天?

该博客主要围绕使用JavaScript判断输入日期是当年的第几天展开,聚焦于利用JS实现这一功能,属于前端开发中日期处理的相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<input type="text" id="a"/>年
		<input type="text" id="b"/>月
		<input type="text" id="c"/>日
		<input type="button" id="btn" value="测试"/>
	</body>
</html>
	var btn = document.getElementById("btn");

	btn.onclick = function(){
		var a = Number(document.getElementById("a").value);
		var b = Number(document.getElementById("b").value);
		var c = Number(document.getElementById("c").value);
		alert(getday(a,b,c));
	}	
   function getday(x,y,z){  //年/月/日
	   var n = 0;
        if(x%4==0 && x%400==0){
            if(y==1)
                n=z;
            else if(y==2)
                n=parseInt(31)+parseInt(z);
            else if(y==3)
                n=parseInt(60)+parseInt(z);
            else if(y==4)
                n=parseInt(91)+parseInt(z);
            else if(y==5)
                n=parseInt(121)+parseInt(z);
            else if(y==6)
                n=parseInt(152)+parseInt(z);
            else if(y==7)
                n=parseInt(182)+parseInt(z);
            else if(y==8)
                n=parseInt(213)+parseInt(z);
            else if(y==9)
                n=parseInt(244)+parseInt(z);
            else if(y==10)
                n=parseInt(274)+parseInt(z);
            else if(y==11)
                n=parseInt(305)+parseInt(z);
            else if(y==12)
                n=parseInt(335)+parseInt(z);
        }
        if(x%4!=0 || x%400!=0){            
            if(y==1)
                n=z;
            else if(y==2)
                n=parseInt(31)+parseInt(z);
            else if(y==3)
                n=parseInt(59)+parseInt(z);
            else if(y==4)
                n=parseInt(90)+parseInt(z);
            else if(y==5)
                n=parseInt(120)+parseInt(z);
            else if(y==6)
                n=parseInt(151)+parseInt(z);
            else if(y==7)
                n=parseInt(181)+parseInt(z);
            else if(y==8)
                n=parseInt(212)+parseInt(z);
            else if(y==9)
                n=parseInt(243)+parseInt(z);
            else if(y==10)
                n=parseInt(273)+parseInt(z);
            else if(y==11)
                n=parseInt(304)+parseInt(z);
            else if(y==12)
                n=parseInt(334)+parseInt(z);
        }
        return x + "年的第"+ n + "天";
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值