一个计算“整星期日期”的小功能
时间:2010-08-24 来源:大虾米
代码
<html>
<head>
<title>日期插件</title>
</head>
<body>
<script type="text/javascript">
function myDate() {
function formatDate(d) {
var _todayDate = d.getDate();
var _year = d.getYear();
!document.addEventListener || (_year += 1900);
var _month = d.getMonth()+1;
return _year+"-"+_month+"-"+_todayDate;
}
var now = new Date(), dd = new Date();
var n = now.getDay();
function check(date,m) {
for (var i = 0;i < 7; i++) {
var datevalue = now.getDate()-n+m+i+1;
var month = now.getMonth()+1;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (datevalue > 31) {
if ( m < 0 && month - 1 != 2) {
dd.setDate(31-(30-datevalue));
dd.setMonth(month)
}
else
{
dd.setDate(datevalue - 31);
dd.setMonth(month);
}
}
else
{
dd.setDate(datevalue);
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (datevalue > 30) {
if ( m < 0 && month - 1 != 2) {
dd.setDate(31-(30-datevalue));
dd.setMonth(month)
}
else
{
dd.setDate(datevalue - 30);
dd.setMonth(month);
}
}
else
{
dd.setDate(datevalue);
}
}
else if (month == 2) {
if (datevalue > 28) {
dd.setDate(datevalue - 28);
m >= 0 ? dd.setMonth(month+1) : dd.setMonth(month-1);
}
else
{
dd.setDate(datevalue);
}
}
date.push(formatDate(dd));
}
}
return {
currWeek : function () {
var date = [];
check(date, 0);
return date;
},
nextWeek : function () {
var date = [];
check(date, 7);
return date;
},
prevWeek : function () {
var date = [];
check(date, -7);
return date;
}
}
}
var s = new myDate();
document.write("上星期的每天日期:"+s.prevWeek()+"<br />"+"本星期的每天日期;"+s.currWeek()+"<br />"+"下星期的每天日期:"+s.nextWeek());
</script>
</body>
</html>
相关阅读 更多 +