1、输入某年某月某日,判断这一天是这一年的第几天。
function isLeap(year) { //判断是否是闰年
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
return true;
}
return false;
}
function getDays(year, month, day) {
var days = day;//总共有多少天,就是第几天。
if (month === 1) { //如果是1月的话直接返回天数。
return days;
}
var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];//数组下面的for循环语句中的i是索引;
for (var i = 0; i < month - 1; i++) { //i代表的是月份,遍历出每个月对应的天数
days += months[i];//取出当月之前所有月份天数之和
}
//2月份的天数是28还是29? 判断当前是不是闰年。闰年29平年28.
if (month > 2 && isLeap(year)) { //用来判断上面计算出来的天数用不用再加一天。
days++;
}
return days;
}
console.log(getDays(2018, 1, 11));