js封装年月日时分秒,闰年,学年

window.onload = function () {
    var today=new Date(); 
    var gYear=today.getFullYear();
    var gMonth=today.getMonth()+1;
    var gDate=today.getDate();
    var gDay=today.getDay();
    var gHour=today.getHours();
    var gMinute=today.getMinutes();
    var gSecond=today.getSeconds();
    //添加星期
    myWeek=new Array(6);
    myWeek[0]="星期日"
    myWeek[1]="星期一"
    myWeek[2]="星期二"
    myWeek[3]="星期三"
    myWeek[4]="星期四"
    myWeek[5]="星期五"
    myWeek[6]="星期六"
    
    if (gDay==0 | gDay==6)
    {
    mweek=myWeek[gDay];
    }
    else
    {
    mweek=myWeek[gDay];
    }
    //判断是否是闰年
    function isLeapYear(year) { 
        return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); 
    } 
    //获取一年中所有月份以及天数
    function getMonthDays(year, month) { 
        return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28); 
    } 

    var yearFirstDay = new Date(gYear, gMonth-1, gDate).getDay() || 7; 

    //判断是那个学年度
    var xuenian;
    if (gMonth>=9)
    {
        nian=gYear;
        xuenian=gYear+"-"+(gYear+1)+"学年度 ";
    
    }
    else if (gMonth<=8)
    { 
        nian=gYear-1;
        xuenian=(gYear-1)+"-"+gYear+"学年度 ";
    }


    //判断是上半学期还是下半学期
    var xueqi="";
    var week="";
    var totalday="";
    if (gMonth>=9|gMonth<=1)
    {
    //计算实际周,(第一学期部分)约定,每年9月1日开学,为第一周,从下一个星期一开始算第二周。
        xueqi="第一学期 ";
        var firstdate=new Date(nian,8,4);
        var lDay=firstdate.getDay();
        var TheDate=new Date(nian,8,4);
    
        var secDate=new Date();
        var dft = secDate-TheDate;
        var totalday=Math.floor(dft/(1000*60*60*24));
    
        days=totalday+1;
           
            var yearFirstDay = new Date(nian,8,4).getDay() || 7; 
        
            //判断第几周
            if (yearFirstDay == 7) { 
                    week = Math.ceil(days/yearFirstDay); 
                } else { 
                    days -= (7 - yearFirstDay + 1); 
                    week = Math.ceil(days/7) + 1; 
                    //totalday = Math.max(totalday, 1); 
            } 
        
    }else if (2<=gMonth<=8) {
        var firstdate=new Date(gYear,2,3); //约定第二学期开学第一天
        var lDay=firstdate.getDay();
        var secDate=new Date();
        if ( firstdate<=secDate)
        {
        
            xueqi="第二学期 ";
            var dft = secDate-firstdate;  
            
            var totalday=Math.floor(dft/(1000*60*60*24)); 
            
            //相差多少天
            days=totalday+1;
            var yearFirstDay = new Date(gYear,2,5).getDay() || 7; 
            //判断第几周
            if (yearFirstDay == 7) { 
                    week = Math.ceil(days/yearFirstDay); 
                } else { 
                    days -= (7 - yearFirstDay + 1); 
                    week = Math.ceil(days/7) + 1; 
            } 
        }
        else
        {
        str="寒假,正在放假中";
        }
    }
    //添加到DOM元素中
    document.getElementById('weekCalendar').innerHTML = week
    // $('#weekCalendar').html(week);
    document.getElementById('week').innerHTML = mweek



    // 获取当前时间
    CurentTime()
    function CurentTime() {
        var now = new Date();
        var year = now.getFullYear();       
        var month = now.getMonth() + 1;    
        var day = now.getDate();            
        var today = now.getDay();

        var hh = now.getHours();            
        var mm = now.getMinutes();    
        var ss = now.getSeconds();      
        var clock = year + "-";
        if(month < 10) clock += "0";
        clock += month + "-";
        
        if(day < 10) clock += "0";
        clock += day + " ";
        
        if(hh < 10) clock += "0";
        clock += hh + ":";
        if (mm < 10) clock += '0'; 
        clock += mm; 
        document.getElementById('year').innerHTML = clock
    }
    setInterval(function(){CurentTime()},1000)
}

 

posted @ 2019-04-10 16:17  若如霜寒  阅读(401)  评论(0编辑  收藏  举报