通過當前日期求得本周開始和結束的日期
用java或在jsp頁面上解決﹗
<通過當前日期求得本周開始和結束的日期>
不需要通過button,eg:今天是2007-03-20
頁面上應顯示 时间段:2007-03-19 到 2007-03-25
[解决办法]
唉..... 妈妈说做好人要做到底
<html>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<head> <title> DateDamo </title>
</head>
<body>
<form method= "post " action= "ttt.jsp " name= "frmquery ">
<center>
<table border= "0 " width= "100% " height= "63 ">
<tr height= "31 ">
<td width= "14% " align= "middle "> Sun </td>
<td width= "14% " align= "middle "> Mon </td>
<td width= "14% " align= "middle "> Tue </td>
<td width= "14% " align= "middle "> Wen </td>
<td width= "14% " align= "middle "> Thu </td>
<td width= "15% " align= "middle "> Fri </td>
<td width= "15% " align= "middle "> Sat </td>
</tr>
<tr height= "32 ">
<td id= "h1 " align= "middle "> </td>
<td id= "h2 " align= "middle "> </td>
<td id= "h3 " align= "middle "> </td>
<td id= "h4 " align= "middle "> </td>
<td id= "h5 " align= "middle "> </td>
<td id= "h6 " align= "middle "> </td>
<td id= "h7 " align= "middle "> </td>
</tr>
</table>
</center>
</TABLE>
<INPUT type=button value=上周 width=20 onClick= "javascript:front(); ">
<INPUT type=button value=本周 width=20 onClick= "javascript:now(); ">
<INPUT type=button value=下周 width=20 onClick= "javascript:next(); ">
</form>
</body>
</HTML>
<script language=javascript>
var defaultFormat = "yyyy年MM月dd日 hh时mm分ss秒 ";//改成你想要的日期格式
var week;
function window.onload()
{
week = new Week();
now();
}
function front()
{
week.front();
document.getElementById( "h1 ").innerText = week.firstDay.Format(defaultFormat);
document.getElementById( "h7 ").innerText = week.firstDay.Format(defaultFormat);
}
function now()
{
week.now();
document.getElementById( "h1 ").innerText = week.firstDay.Format(defaultFormat);
document.getElementById( "h7 ").innerText = week.firstDay.Format(defaultFormat);
}
function next()
{
week.next();
document.getElementById( "h1 ").innerText = week.firstDay.Format(defaultFormat);
document.getElementById( "h7 ").innerText = week.firstDay.Format(defaultFormat);
}
function Week()
{
this.thedate = new Date();
this.firstDay = new Date();
this.lastDay = new Date();
this.days = new Array();
this.now = function(){this.thedate = new Date();this.setweek();}
this.front = function(){this.thedate.setDate(this.thedate.getDate()-7);this.setweek();}
this.next = function(){this.thedate.setDate(this.thedate.getDate()+7);this.setweek();}
this.setweek = function()
{
for(i=0;i <7;i++)
this.days[i] = new Date(this.thedate.getFullYear(),this.thedate.getMonth(),this.thedate.getDate()-this.thedate.getDay()+i)
this.firstDay = this.days[0];
this.lastDay = this.days[6];
}
this.setweek();
}
Date.prototype.Format = function(informat) //author: meizz
{
var inputStr = " ";
var format = this.format;
if(informat!=null)format = informat;
var o = {
"M+ " : this.getMonth()+1,
"d+ " : this.getDate(),
"h+ " : this.getHours(),
"H+ " : this.getHours(),
"m+ " : this.getMinutes(),
"s+ " : this.getSeconds()
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+ " ").substr(4 - RegExp.$1.length));
for(var k in o)if(new RegExp( "( "+ k + ") ").test(format))
{
format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k]:( "00 "+ o[k]).substr(( " "+ o[k]).length));
}
return format;
}
</script>
[解决办法]
<div style= "font-size:12px; color:green; ">
上周: <span id= "oLastWeek "> </span> <br>
本周: <span id= "oThisWeek "> </span> <br>
下周: <span id= "oNextWeek "> </span>
</div>
<script language=javascript>
function Week()
{
this.thedate = new Date();
this.firstDay = this.thedate;
this.lastDay = this.thedate;
this.thisweek = function(){this.setweek();}
this.lastweek = function(){this.thedate.setDate(this.thedate.getDate()-7);this.setweek();}
this.nextweek = function(){this.thedate.setDate(this.thedate.getDate()+14);this.setweek();}
this.setweek = function()
{
this.firstDay = new Date(this.thedate.getFullYear(),this.thedate.getMonth(),this.thedate.getDate()-this.thedate.getDay()+1);
this.lastDay = new Date(this.thedate.getFullYear(),this.thedate.getMonth(),this.thedate.getDate()-this.thedate.getDay()+7) ;
}
this.setweek();
}
var week = new Week();
thisweek();
lastweek();
nextweek();
function lastweek()
{
week.lastweek();
document.getElementById( 'oLastWeek ').innerText=week.firstDay.toLocaleString().slice(0,-8)+ '-- '+week.lastDay.toLocaleString().slice(0,-8);
//ojbString.slice(start[,end]),其中end为负数时从右向左数,这里作用是去掉日期后面的时间显示。
}
function thisweek()
{
week.thisweek();
document.getElementById( 'oThisWeek ').innerText=week.firstDay.toLocaleString().slice(0,-8)+ '-- '+week.lastDay.toLocaleString().slice(0,-8);
}
function nextweek()
{
week.nextweek();
document.getElementById( 'oNextWeek ').innerText=week.firstDay.toLocaleString().slice(0,-8)+ '-- '+week.lastDay.toLocaleString().slice(0,-8);
}
</script>