• Date对象的一些相关函数


    传入两个Date类型的日期,求出它们相隔多少天。

        var getDatePeriod = function(start,finish){
             return Math.abs(start*1-finish*1)/60/60/1000/24;
        }
    

    传入一个Date类型的日期,求出它所在月的第一天。

        var getFirstDateInMonth= function(date){
             return new Date(date.getFullYear(),date.getMonth(),1);
        }
    

    传入一个Date类型的日期,求出它所在月的最后一天。

        var getLastDateInMonth= function(date){
             return new Date(date.getFullYear(),date.getMonth()+1,0);
        }
    

    传入一个Date类型的日期,求出它所在季度的第一天。

        var getFirstDateInQuarter= function(date){
             return new Date(date.getFullYear(),~~(date.getMonth()/3)*3,1);
        }
    

    传入一个Date类型的日期,求出它所在季度的最后一天。

        var getFirstDateInQuarter= function(date){
             return new Date(date.getFullYear(),~~(date.getMonth()/3)*3+3,0);
        }
    

    判断是否为闰年。

    Date.prototype.isLeapYear = function()   {  
          return new Date(this.getFullYear(),2,0).getDate() == 29;
    }
    

    取得当前周某星期几的Date对象,参数为星期号(星期一为1,星期二为2……星期日为7)。

    var getWeekDay = function(num){
       var now = new Date(new Date.setHours(0,0,0,0));
       var day = now.getDay() || 7;
       var delta = Math.abs(day-num) * 60 * 60 * 1000 * 24;
       if(day > num){
         return new Date(now - delta);
       }else if(day < num){
         return new Date(now + delta);
       }else{
         return now;
       }
    }
    // alert(getWeekDay(1).toLocaleDateString())
    
    //2011.5.30更新
          function daysInMonth ( year, month ) {
            var num = new Date();
            num.setFullYear( year, (month == 12) ? 1 : month + 1, 0 );
            return num.getDate();
          }
    
          function dateDif( inDate1, inDate2 ) {
            var ret = new Array();
            var date1 = new Date( (inDate1 <= inDate2) ? inDate1 : inDate2 );
            var date2 = new Date( (inDate1 <= inDate2) ? inDate2 : inDate1 );
            var temp1 = new Date( date2.getFullYear(), date1.getMonth(), date1.getDate() );
            var temp2 = new Date( date2.getFullYear(), date2.getMonth() - (date1.getDate() > date2.getDate() ? 1 : 0 ), date1.getDate() );
            /*Y*/ ret[0] = date2.getFullYear() - date1.getFullYear() - (date2 < temp1 ? 1 : 0);
            /*M*/ret[1] = temp2.getMonth() - temp1.getMonth() + (temp1.getMonth() > temp2.getMonth() ? 12 : 0 );
            /*D*/ret[2] = date2.getDate() - temp2.getDate() + (temp2.getDate() > date2.getDate() ? daysInMonth(temp2.getFullYear(), temp2.getMonth()) : 0);
            return ret;
          }
    
    //2011.5.30更新
    function getDaysInMonth1(date) {
    	switch (date.getMonth()) {
    	case 0:
    	case 2:
    	case 4:
    	case 6:
    	case 7:
    	case 9:
    	case 11:
    		return 31;
    	case 1:
    		var y = date.getFullYear();
    		return y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ? 29 : 28;
    	default:
    		return 30;
    	}
    }
    
    var getDaysInMonth2 = (function() {
    	var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    
    	function isLeapYear(date) {
    		var y = date.getFullYear();
    		return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
    	}
    	return function(date) { // return a closure for efficiency
    		var m = date.getMonth();
    
    		return m == 1 && isLeapYear(date) ? 29 : daysInMonth[m];
    	};
    })();
    
    function getDaysInMonth3(date) {
    	return new Date(date.getFullYear(),date.getMonth()+1,0).getDate();
    }
    
    function test(f) {
    	var d = new Date;
    	var date = new Date(2000, 1, 2);
    	for (var i = 0; i < 80000; i++)
    		f(date);
    	return(new Date - d);
    }
    function test2(f) {
    	var d = new Date;
    	for (var i = 0; i < 80000; i++)
    		f(d);
    	return(new Date - d);
    }
    alert([test(getDaysInMonth1), test(getDaysInMonth2), test(getDaysInMonth3)]);
    alert([test2(getDaysInMonth1), test2(getDaysInMonth2), test2(getDaysInMonth3)]);
    

    判定字符串2009/2/23(或者2009/02/23)是否合法

    var test = function(str){
       var arr = str.split("/")
       var m = Number(arr[1])-1
       var d = new Date(Number(arr[0]),m,Number(arr[2]) )
       if(isNaN(d))
         return false;
       if(isNaN(d.getDate()))
         return false;
       return d.getMonth() === m
    }
    test("2009/2/25")//true
    test("2009/2/29")//false
    test("2009/2/32")//false
    test("2009/02/15")//true
    //附上abcd的
    function test(s){
    	return /^(\d{4})\/(\d\d?)\/(\d\d?)$/.test(s) && s.replace(/^(\d{4})\/(\d\d?)\/(\d\d?)$/,function(a,b,c,d){
    		var m = parseInt(c,10);
    		if (m < 1 || m > 12) return false;
    		var d = parseInt(d,10);
    		return d > 0 && d <= (
    			m == 2 ? (m = parseInt(b, 10), m % 4 == 0 && m % 100 != 0 || m % 400 == 0) ? 29 : 28 :
    			m == 4 || m == 6 || m == 9 || m == 11 ? 30 : 31);
    	});
    }
    
    
  • 相关阅读:
    bzoj1818 [Cqoi2010]内部白点
    bzoj4001 [TJOI2015]概率论
    bzoj3997 [TJOI2015]组合数学
    bzoj3193 [JLOI2013]地形生成
    bzoj4869 [Shoi2017]相逢是问候
    bzoj4868 [Shoi2017]期末考试
    CF421D Bug in Code
    CCPC-WFinal-女生专场
    CF915F Imbalance Value of a Tree
    soj考试2
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/1827784.html
Copyright © 2020-2023  润新知