标签:
在JS练习题中,有这么一道:
请计算某天是这一年的第几天。
首先,我们要看此年是否闰年
var IsLeapYear = function (year) { var flag = false; if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) flag = true; return flag; };
for (var i = 0; i < month - 1; i++) { iDay += months[i]; }其中months为数组
var months = new Array(31, day2, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);day2如果是闰年赋值29,否则28。
if (IsLeapYear(iYear)) { day2 = 29; } else { day2 = 28; }
calDay.js
/* * xiaobin in SYSIT schools completed */ var MY = {}; MY.DayOfYear = function(year, month, day) { var day2 = 0; var iDay = 0; var date = new Date(); date.setFullYear(year, month, day); var iYear = date.getFullYear(); var IsLeapYear = function (year) { var flag = false; if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) flag = true; return flag; }; if (IsLeapYear(iYear)) { day2 = 29; } else { day2 = 28; } var months = new Array(31, day2, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); for (var i = 0; i < month - 1; i++) { iDay += months[i]; } iDay += day; SetInputVal(iDay); return iDay; }; function SetInputVal (value) { var val = document.getElementById("daySum"); val.value = value; };
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example01</title> <script type="text/javascript" src="calDay.js" > </script> </head> <body> <p> <br> <input type="text" id="daySum" > <input type="button" id="test02" value="cal day" onClick="MY.DayOfYear(2000, 2, 8)"> </p> </body> </html>
标签:
原文地址:http://blog.csdn.net/xiaobin_hlj80/article/details/45103061