關於日期的操作

一、獲取某年各月的最後一天

   function getMonthLastDay(year){
        var lastMonthDay = [];
        year = ( year < 1900 ) ? ( 1900 +year ) : year;
        var tempDate = new Date();  
        var tempYear = tempDate.getYear();
        for(month=0; month<12; month++){
            tempDate = new Date();  
            tempDate.setYear(year);  
            tempDate.setMonth(month+1);  
            tempDate.setDate(0);  
            tempYear = tempDate.getYear();  
            tempYear = ( tempYear < 1900 ) ? ( 1900 +tempYear ) : tempYear;  
            lastMonthDay.push(tempYear+"-"+(tempDate.getMonth()+1)+"-"+tempDate.getDate());
        }
        
        return lastMonthDay;
      }

例如: 獲取2014年每個月最後一天的日期

document.write(getMonthLastDay('2014').join(','));

返回結果:2014-1-31,2014-2-28,2014-3-31,2014-4-30,2014-5-31,2014-6-30,2014-7-31,2014-8-31,2014-9-30,2014-10-31,2014-11-30,2014-12-31

二、獲取一段時間日期中是星期五的日期

        這裏用ExtJS的Ext.Date的方法去實現

      

      function getFriday(beginDate, endDate){
    	  friday = [];
    	  date1 = Ext.Date.parse(beginDate, 'Y-m-d');
    	  date2 = Ext.Date.parse(endDate, 'Y-m-d');
    	  //計算兩天相差多少天
    	  diffday = (date2-date1)/86400000;
    	  for(i=1; i<=diffday; i++){
    		  date3 = Ext.Date.add(date1,Ext.Date.DAY, i);
    		  if(date3.getDay()==5){  //星期五
    			  friday.push(Ext.Date.format(date3,'Y-m-d'));
    		  }
    		  
    	  }
    	  return friday;
      }
例如:獲取2014-04-01至2014-06-01這段時間是星期五的日期

document.write(getFriday('2014-04-01','2014-06-01').join(','));
返回結果:2014-04-04,2014-04-11,2014-04-18,2014-04-25,2014-05-02,2014-05-09,2014-05-16,2014-05-23,2014-05-30



   

發佈了35 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章