JavaScript---控制日期上周,下周,上月,下月。

 

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;
 }

 //   &#35745;算日期&#20026;当年的第几周    
 //   &#33719;取   2008 7  5  &#20026;当年的第几周    
 //   返回:  28    
    function weekOfYear(year,   month,   day){    
    //   year       年    
    //   month     月    
    //   day         日    
    //   &#27599;周从周日&#24320;始    
    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;    
      }
 //   通&#36807;周数和星期&#35745;算日期 
 //   &#33719;取    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;    
      }

 //&#38024;&#23545;不同的&#26102;&#38388;&#21333;位做加&#36816;算,得到日期年月日等加数字后的日期
 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;
  }
 }
    //&#33719;取某年某个月的天数
 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;
 }
 //判断一个数字是否&#38384;年
 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);
 }
    //&#26174;示天
 function showToday(format_t){
       $("t_date").innerHTML = format_t;
 }
 //&#26174;示周
 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];
 }
 //&#26174;示月
 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值