http://www.cssrain.cn/demo/lab/manageDate.html
<script language="javascript" >
//格式化日期格式
//var dates = format( new Date() , "yyyy-MM-DD hh:mm:ss");
function format(date , formatStr){
var str = formatStr;
str=str.replace(/yyyy|YYYY/,date.getFullYear());
str=str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():"0" + (date.getYear() % 100));
str=str.replace(/MM/,date.getMonth()>8?(date.getMonth()+1).toString():"0" + (date.getMonth()+1));
str=str.replace(/M/g,date.getMonth()+1);
str=str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():"0" + date.getDate());
str=str.replace(/d|D/g,date.getDate());
str=str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():"0" + date.getHours());
str=str.replace(/h|H/g,date.getHours());
str=str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():"0" + date.getMinutes());
str=str.replace(/m/g,date.getMinutes());
str=str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():"0" + date.getSeconds());
str=str.replace(/s|S/g,date.getSeconds());
return str;
}
// 计算日期为当年的第几周
// 获取 2008 7 5 为当年的第几周
// 返回: 28
function weekOfYear(year, month, day){
// year 年
// month 月
// day 日
// 每周从周日开始
var date1 = new Date(year, 0, 1);
var date2 = new Date(year, month-1, day, 1);
var dayMS = 24*60*60*1000;
var firstDay = (7-date1.getDay())*dayMS;
var weekMS = 7*dayMS;
date1 = date1.getTime();
date2 = date2.getTime();
return Math.ceil((date2-date1-firstDay)/weekMS)+1;
}
// 通过周数和星期计算日期
// 获取 2005 年第 37 周的周六的日期 。 (0-6, 0代表周日)
// 返回: 2005年9月10号
function dateFromWeek(year, week, day){
// year 年
// week 周
// day 星期 (0-6, 0代表周日)
var date1 = new Date(year, 0, 1);
var dayMS = 24*60*60*1000;
var firstDay = (7-date1.getDay())*dayMS;
var weekMS = (week-2)*7*dayMS;
var result = date1.getTime()+firstDay+weekMS+day*dayMS;
date1.setTime(result);
// return date1.toLocaleDateString();
return date1;
}
//针对不同的时间单位做加运算,得到日期年月日等加数字后的日期
function dateAdd( date , interval , number) {
switch(interval) {
case "y" :
date.setFullYear(date.getFullYear()+number);
return date;
case "q" :
date.setMonth(date.getMonth()+number*3);
return date;
case "m" :
date.setMonth(date.getMonth()+number);
return date;
case "w" :
date.setDate(date.getDate()+number*7);
return date;
case "d" :
date.setDate(date.getDate()+number);
return date;
case "h" :
date.setHours(date.getHours()+number);
return date;
case "m" :
date.setMinutes(date.getMinutes()+number);
return date;
case "s" :
date.setSeconds(date.getSeconds()+number);
return date;
default :
date.setDate(d.getDate()+number);
return date;
}
}
//获取某年某个月的天数
function monthDays(iYear,iMonth){
iYear = parseInt(iYear);
iMonth = parseInt(iMonth);
var iDay = 31;
if((iMonth==4||iMonth==6||iMonth==9||iMonth==11)&&iDay == 31){
iDay=30;
}
if(iMonth==2 ){
if(isLeapYear(iYear)){
iDay = 29;
}else {
iDay = 28;
}
}
return iDay;
}
//判断一个数字是否闰年
function isLeapYear(iYear) {
if(iYear+"" == "undefined" || iYear+""== "null" || iYear+"" == "") {
return false;
}
iYear = parseInt(iYear);
if((iYear % 4 == 0 && iYear % 100 != 0) || iYear % 400 == 0){
return true
}else{
return false
};
}
//DOM操作
function $(id){
return document.getElementById(id);
}
//显示天
function showToday(format_t){
$("t_date").innerHTML = format_t;
}
//显示周
function showWeek(today){
var week = weekOfYear(today.getYear() , today.getMonth()+1 , today.getDate() );
var xingqiri = dateFromWeek( today.getYear() , week , 0);
var xingqiliu = dateFromWeek( today.getYear() , week , 6);
var format_xingqiri = format( xingqiri , "yyyy/MM/DD");
var format_xingqiliu = format( xingqiliu , "yyyy/MM/DD");
$("w_date").innerHTML = format_xingqiri+"到"+format_xingqiliu;
return [xingqiri,xingqiliu];
}
//显示月
function showMonth(today,format_t){
var m_days = monthDays( today.getYear() , today.getMonth()+1 );
var m_YearAndMonth = ( format_t.substring(0,4) )+"/"+( format_t.substring(5,7) );
$("m_date").innerHTML = m_YearAndMonth+"/01"+"到"+m_YearAndMonth+"/"+m_days;
}
window.onload = function(){
var today =new Date();
var format_t = format( today , "yyyy/MM/DD");
showToday(format_t);
var WeekReturnValue = showWeek(today);
showMonth(today,format_t);
//上周
document.getElementById("prev").οnclick=function(){
var prev_xingqiri = dateAdd( WeekReturnValue[0] , "d" , -7 ) ;
var prev_xingqiliu = dateAdd( WeekReturnValue[1] , "d" , -7 ) ;
format_xingqiri = format( prev_xingqiri , "yyyy/MM/DD");
format_xingqiliu = format( prev_xingqiliu , "yyyy/MM/DD");
$("w_date").innerHTML = format_xingqiri+"到"+format_xingqiliu;
}
//下周
document.getElementById("next").οnclick=function(){
var next_xingqiri = dateAdd( WeekReturnValue[0] , "d" , 7 ) ;
var next_xingqiliu = dateAdd( WeekReturnValue[1] , "d" , 7 ) ;
format_xingqiri = format( next_xingqiri , "yyyy/MM/DD");
format_xingqiliu = format( next_xingqiliu , "yyyy/MM/DD");
$("w_date").innerHTML = format_xingqiri+"到"+format_xingqiliu;
}
//本周
document.getElementById("current").οnclick=function(){
WeekReturnValue = showWeek(today);
}
//上月
document.getElementById("prev_m").οnclick=function(){
var prev_monthFirst = dateAdd( today , "m" , -1 ) ;
var prev_month_LastDay = monthDays( prev_monthFirst.getYear() , prev_monthFirst.getMonth()+1 );
prev_monthFirst = format( prev_monthFirst , "yyyy/MM/DD");
m_YearAndMonth = ( prev_monthFirst.substring(0,4) )+"/"+( prev_monthFirst.substring(5,7) );
$("m_date").innerHTML = m_YearAndMonth+"/01"+"到"+m_YearAndMonth+"/"+prev_month_LastDay;
today = new Date( m_YearAndMonth+"/01");
}
//下月
document.getElementById("next_m").οnclick=function(){
var next_monthFirst = dateAdd( today , "m" , 1 ) ;
var next_month_LastDay = monthDays( next_monthFirst.getYear() , next_monthFirst.getMonth()+1 );
next_monthFirst = format( next_monthFirst , "yyyy/MM/DD");
m_YearAndMonth = ( next_monthFirst.substring(0,4) )+"/"+( next_monthFirst.substring(5,7) );
$("m_date").innerHTML = m_YearAndMonth+"/01"+"到"+m_YearAndMonth+"/"+next_month_LastDay;
today = new Date( m_YearAndMonth+"/01");
}
//本月
document.getElementById("current_m").οnclick=function(){
showMonth(today,format_t);
today = new Date();
}
}
</script>
当天:<span id="t_date"></span><br/><br/>
周:<span id="w_date"></span><br/><br/>
月:<span id="m_date"></span><br/><br/>
<button id="prev">上周</button>
<button id="current">本周</button>
<button id="next">下周</button><br/><br/>
<button id="prev_m">上月</button>
<button id="current_m">本月</button>
<button id="next_m">下月</button>