关于日期的操作

一、获取某年各月的最后一天

   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



   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值