正在加载中...

关闭
请选择需要拨打的号码
关闭
请根据问题类型选择QQ咨询

已解决:有没有好用的javascript日期运算常用函数/jquery日期格式化和计算函数?

已解决:有没有好用的javascript日期运算常用函数/jquery日期格式化和计算函数?

 在前端应用中,常常会涉及到日期的加减法和其他运算问题,javascript没有像formatedatetime()和dateadd(),datediff()这样的系统内置函数,所以这里给大家分享一下常用的自定制的javascript日期运算函数,希望有帮助。


  // 计算日期  
       function _calculateDate(wantDate){
        var newWantDate = {};
        var currentTime = new Date();
        // 今日
        var currentDate = timeStamp2String(currentTime);
        if (wantDate == "thisDay") {
            newWantDate[0] = currentDate;
            newWantDate[1] = currentDate;
            return newWantDate;
        }
        //alert(currentDate);
        // 昨日
        var lastDate = timeStamp2String(_addDate('4','-1',currentTime));
        if (wantDate == "lastDay") {
            newWantDate[0] = lastDate;
            newWantDate[1] = lastDate;
            return newWantDate;
        }
        //alert(lastDate);
        var currentWeek = _getCurrentWeek();
        // 本周第一天
        var currentWeekFirstDate = timeStamp2String(currentWeek[0]); 
        //alert(currentWeekFirstDate)
        // 本周最后一天
        var currentWeekEndDate = timeStamp2String(currentWeek[1]); 
        //alert(currentWeekEndDate)
        if (wantDate == "thisWeek") {
            newWantDate[0] = currentWeekFirstDate;
            newWantDate[1] = currentWeekEndDate;
            return newWantDate;
        }
        // 上周第一天
        var lastWeekFirstDate = timeStamp2String(_addDate('4','-7',currentWeek[0]));
        //alert(lastWeekFirstDate)
        // 上周最后一天
        var lastWeekEndDate = timeStamp2String(_addDate('4','-1',currentWeek[0]));
        //alert(lastWeekEndDate)
        if (wantDate == "lastWeek") {
            newWantDate[0] = lastWeekFirstDate;
            newWantDate[1] = lastWeekEndDate;
            return newWantDate;
        }
        // 前两周第一天
        var last2WeekFirstDate = timeStamp2String(_addDate('4','-14',currentWeek[0]));
        if (wantDate == "last2Week") {
         newWantDate[0] = last2WeekFirstDate;
            newWantDate[1] = lastWeekEndDate;
            return newWantDate;
        }
        var datePartArray = currentDate.split('-');
        // 本月开始的第一天
        var curMonthFirstDay = new Date(datePartArray[0],datePartArray[1]-1);
        var curMonthFirstDate = timeStamp2String(curMonthFirstDay);
        //alert(curMonthFirstDate);
        //下月开始的第一天
        var nextMonthFirstDay = _addDate('5','1',curMonthFirstDay);
        //本月最后一天
        var curMonthEndDay = new Date(nextMonthFirstDay.getTime()-1);
        var curMonthEndDate = timeStamp2String(curMonthEndDay);
        //alert(curMonthEndDate);
        if (wantDate == "thisMonth") {
            newWantDate[0] = curMonthFirstDate;
            newWantDate[1] = curMonthEndDate;
            return newWantDate;
        }
        // 上月开始的第一天
        var lastMonthFirstDay = _addDate('5','-1',curMonthFirstDay);
        var lastMonthFirstDate = timeStamp2String(lastMonthFirstDay);
        //alert(lastMonthFirstDate);
        // 上月最后一天
        var lastMonthEndDay = new Date(curMonthFirstDay.getTime()-1);
        var lastMonthEndDate = timeStamp2String(lastMonthEndDay);
        //alert(lastMonthEndDate);
        if (wantDate == "lastMonth") {
            newWantDate[0] = lastMonthFirstDate;
            newWantDate[1] = lastMonthEndDate;
            return newWantDate;
        }
        // 最近三个月的第一天
        var last3MonthFirstDay = _addDate('5','-2',curMonthFirstDay);
        var last3MonthFirstDate = timeStamp2String(last3MonthFirstDay);
        if (wantDate == "last3Month") {
         newWantDate[0] = last3MonthFirstDate;
            newWantDate[1] = curMonthEndDate;
            return newWantDate;
        }
        // 最近半年的第一天
        var last6MonthFirstDay = _addDate('5','-5',curMonthFirstDay);
        var last6MonthFirstDate = timeStamp2String(last6MonthFirstDay);
        if (wantDate == "last6Month") {
         newWantDate[0] = last6MonthFirstDate;
            newWantDate[1] = curMonthEndDate;
            return newWantDate;
        }
        // 最近一年的第一天
        var last12MonthFirstDay = _addDate('5','-11',curMonthFirstDay);
        var last12MonthFirstDate = timeStamp2String(last12MonthFirstDay);
        if (wantDate == "last12Month") {
         newWantDate[0] = last12MonthFirstDate;
            newWantDate[1] = curMonthEndDate;
            return newWantDate;
        }
        //下下月开始的第一天
        var nnextFirstDay=_addDate('5','2',curFirstDay);
        //下月最后一天
        var nextEndDay= new Date(nnextFirstDay.getTime()-1);
    }
    // 计算本周
    function _getCurrentWeek(){
        //获取当前时间  
        var currentDate = new Date(); 
        //返回date是一周中的某一天  
        var week=currentDate.getDay(); 
        //一天的毫秒数  
        var millisecond=1000*60*60*24; 
        //减去的天数  
        var minusDay=week!=0?week-1:6; 
        //本周 周一  
        var monday=new Date(currentDate.getTime()-(minusDay*millisecond)); 
        //本周 周日  
        var sunday=new Date(monday.getTime()+(6*millisecond)); 
        var thisWeek = {};
        thisWeek[0] = monday;
        thisWeek[1] = sunday;
        return thisWeek;
    }
    // 日期格式化
    function timeStamp2String(time){ 
        var datetime = new Date();     
        datetime.setTime(time); 
        var year = datetime.getFullYear();     
        var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;    
        var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();    
        //var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();    
        //var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();    
        //var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();    
        //return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second; 
        return year + "-" + month + "-" + date; 
    }
    // 日期时间差计算
    function _addDate(type, NumDay, vdate){
        var date=new Date(vdate);
        type = parseInt(type); //类型 
        var lIntval = parseInt(NumDay);//间隔
            switch(type){
                 case 7 ://年
                  date.setYear(date.getYear() + lIntval);
                  break;
                 case 6 ://季度
                  date.setMonth(date.getMonth() + (lIntval * 3) );
                  break;
                 case 5 ://月
                  date.setMonth(date.getMonth() + lIntval);
                  break;
                 case 4 ://天
                  date.setDate(date.getDate() + lIntval);
                  break;
                 case 3 ://时
                  date.setHours(date.getHours() + lIntval);
                  break;
                 case 2 ://分
                  date.setMinutes(date.getMinutes() + lIntval);
                  break;
                 case 1 ://秒
                  date.setSeconds(date.getSeconds() + lIntval);
                  break;
                 default:
            }
        return date;
    }



返回上一页