js一些关于获取日期的操作

1. 获取当前日期是第几周 :
        function isLeapYear(year) {
            return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
        }
        /**
         * 获取某一年份的某一月份的天数
         *
         * @param {Number} year
         * @param {Number} month
         */
        function getMonthDays(year, month) {
            return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
        }
       
       
        function getWeekNumber(y, m, d) {
           
            var now = new Date(y, m - 1, d),
                year = now.getFullYear(),
                month = now.getMonth(),
                days = now.getDate();
            //那一天是那一年中的第多少天
            for (var i = 0; i < month; i++) {
                days += getMonthDays(year, i);
            }
       
            //那一年第一天是星期几
            var yearFirstDay = new Date(year, 0, 1).getDay() || 7;
       
            var week = null;
            if (yearFirstDay == 1) {
                week = Math.ceil(days / yearFirstDay);
            } else {
                days -= (7 - yearFirstDay + 1);
                week = Math.ceil(days / 7) + 1;
            }
           
            var yearWeek= y.toString() + week.toString();
            var yearLastWeek = y.toString() + (week-1).toString();
            console.log("今年的第几周 "+ yearWeek);
            console.log("上周 "+ yearLastWeek );
            return yearWeek;
        }

  2.获取上个月 是几月份 

    function getLastMonth(y,m){
             
             if(m==1){
                 y=y-1;
                 m=12;
             }else if(m<10){
                 m="0"+(m-1);
             }
             var lastMonth = y.toString()+m.toString();
             console.log("这是上个月"+ lastMonth);
             return lastMonth;
             
        }

    3. 获取 当前日期 格式为八位 如: 20160419  

   function getNowDate(){ 
           
             var currentMonth = new Date().getMonth()+1; //当前月上个月
             var currentYear = new Date().getFullYear(); //当前年
             var currentDate = new Date().getDate();     //当前日
            
             if(currentMonth<10){
                 currentMonth="0"+currentMonth;
             }
             if(currentDate<10){
                 currentDate="0"+currentDate;
             }
             var nowday = currentYear.toString()+ currentMonth.toString()+ currentDate.toString();
             
             return nowday;
        }

 4. js 获取 几天后 或者几天前的日期
 

//获取几天后的日期
        function GetDateStr(AddDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
            var y = dd.getFullYear();
            var m = dd.getMonth()+1;//获取当前月份的日期
            var d = dd.getDate();
           
            if(m.length=1){
                m="0"+m.toString();
            }
            if(d.length=1){
                d="0"+d.toString();
            }
           
            return y+m+d;
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值