江南尘缘
发表于 2018-9-23 21:17:54
支持
凌乱大帅比
发表于 2018-10-8 11:53:06
本帖最后由 凌乱大帅比 于 2018-10-8 14:12 编辑
var HowManyDay = function(month,day){
var month_day,how_many_day,s=0;
var month_days = [];
if(month<=12 && month>=1 && day<=31 && day>=1){ //判断输入日期是否合法
if(month==1){how_many_day = day}
else if(month==2){how_many_day = 31+day}
else if(month>=3){
for(var i=1;i<month;i++){ //循环存入每月天数数组
if(i==2){
month_day = 28;
month_days.push(month_day);
}else if(i%2 == 0){
month_day = 30;
month_days.push(month_day);
}else {
month_day = 31
month_days.push(month_day);
}
}
for(var i=0;i<month_days.length;i++){ //计算所有的天数
s+=month_days;
}
console.log(s);
how_many_day = s+day;
}
console.log(month+'月'+day+'日是一年中的第'+how_many_day+'天');
}else{console.log('请输入正常的日期')}
}
HowManyDay(5,9);
没写闰月的判断,目前就这样吧,奇数30天,偶数31天,2月28天{:10_265:}
我还在发愁for中的if哪个排在第一个优化性能,看了版主大大的程序,我决定不纠结了{:10_264:} {:10_264:} {:10_264:}
simble
发表于 2018-10-20 00:32:27
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>判断某年月日为该年的第几天</title>
</head>
<body>
输入(20181010):<input type="text" id="inputDay" />
结果:<input type="text" id="outputDay" />
<input type="button" value="计算" onclick="jisuan();" />
</body>
<script>
function jisuan(){
var inputDay = document.getElementById('inputDay').value;
var year = parseInt(inputDay.slice(0,4));
var mouth = parseInt(inputDay.slice(4,6));
var day = parseInt(inputDay.slice(6,8));
var allDate=0;
if(year%4==0&&year%100!=0){//闰年
if(mouth==1){
allDate = day;
}else if(mouth==2){
allDate = 31+day;
}else if(mouth==3){
allDate = 31+29+day;
}else if(mouth==4){
allDate = 31+29+31+day;
}else if(mouth==5){
allDate = 31+29+31+30+day;
}else if(mouth==6){
allDate = 31+29+31+30+31+day;
}else if(mouth==7){
allDate = 31+29+31+30+31+30+day;
}else if(mouth==8){
allDate = 31+29+31+30+31+30+31+day;
}else if(mouth==9){
allDate = 31+29+31+30+31+30+31+31+day;
}else if(mouth==10){
allDate = 31+29+31+30+31+30+31+31+30+day;
}else if(mouth==11){
allDate = 31+29+31+30+31+30+31+31+30+31+day;
}else if(mouth==12){
allDate = 31+29+31+30+31+30+31+31+30+31+30+day;
}
}else{
if(mouth==1){
allDate = day;
}else if(mouth==2){
allDate = 31+day;
}else if(mouth==3){
allDate = 31+28+day;
}else if(mouth==4){
allDate = 31+28+31+day;
}else if(mouth==5){
allDate = 31+28+31+30+day;
}else if(mouth==6){
allDate = 31+28+31+30+31+day;
}else if(mouth==7){
allDate = 31+28+31+30+31+30+day;
}else if(mouth==8){
allDate = 31+28+31+30+31+30+31+day;
}else if(mouth==9){
allDate = 31+28+31+30+31+30+31+31+day;
}else if(mouth==10){
allDate = 31+28+31+30+31+30+31+31+30+day;
}else if(mouth==11){
allDate = 31+28+31+30+31+30+31+31+30+31+day;
}else if(mouth==12){
allDate = 31+28+31+30+31+30+31+31+30+31+30+day;
}
}
document.getElementById('outputDay').value = allDate;
}
</script>
</html>
人不中二枉少年
发表于 2018-12-4 14:03:34
11
xiaoaiala
发表于 2018-12-20 18:14:40
kankan
453791035
发表于 2018-12-21 09:50:05
54666666666666666666
superlion
发表于 2018-12-21 09:58:20
不错不错
wangdxf
发表于 2019-3-19 11:16:06
{:5_102:}
冰阔落
发表于 2019-3-28 14:25:42
不会{:5_104:}
白无痕
发表于 2019-4-22 19:48:25
var i = prompt("输入年月日(例 2019-1-1):");
var arr = i.split("-");
var days = ;
var day = 0;
for (var j=0; j<Number(arr)-1; j++){
day += Number(days);
}
if (arr%4 != 0 || arr%400 != 0){
day += Number(arr);
}
else{
day += (Number(arr)+1);
}
alert("这一天是这一年的第" + day + "天");
没看效果图,年月日一起输入了{:10_250:}
新手·ing
发表于 2019-4-27 08:05:37
看看
1770843089
发表于 2019-5-31 08:06:28
感谢分享,谢谢
Vage
发表于 2019-7-17 20:17:10
闰年就错了,不知道干嘛
掵ф伫錠
发表于 2019-12-9 20:54:20
本帖最后由 掵ф伫錠 于 2019-12-9 20:57 编辑
var kaiguan = true;
var count = 0;
while (kaiguan) {
var yuefen = Number(prompt("请输入月份"));
if (!yuefen) {
alert("请输入数字月份")
} else {
while (kaiguan) {
var tianshu = Number(prompt("请输入几号"));
if (!tianshu) {
alert("请输入数字几号")
} else {
break;
}
}
break;
}
}
var month = ;
for (const key in month) {
if (key == yuefen) {
break;
} else {
count = count + month ;
}
}
alert("第" + (count + tianshu) + "天");
sensems
发表于 2020-1-8 18:14:26
function getDays(year,month,day) {
let arr = ;
if (year % 4 == 0 && year % 100 != 0) {
arr = ;
}
for (let i = 0; i <= month-2; i++) {
day += arr;
}
return day;
}
gh12345
发表于 2020-3-15 15:41:25
学一下
宇治松千夜
发表于 2020-3-16 14:45:20
康康
落寞少年
发表于 2020-10-29 14:12:54
var years = prompt("请输入年份(如:2000):");
var month = prompt("请输入月份(如:6):");
var day = prompt("请输入日期(如:20):");
var days = 0;
for(i=1;i<month;i++){
if(i==4||i==6||i==9||i==11 )days+=30;
if(i==2){
if(!(typeof(years/400)=="int"||(typeof(years/4)=="int"&&typeof(years/100)!="int")))days+=28;
else{days+=29;}
}
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)days+=31;
}
days+=parseInt(day);
confirm(month+"月 "+day+"日 是"+years+"年的第"+days+"天!");
TI8的maybe本人
发表于 2022-1-25 13:18:45
我看看有没有简单点的方法
jack6666
发表于 2022-10-12 22:26:30
1