根据时间获取相应当月每周的分布

来源:互联网 发布:孟加拉国知乎 编辑:程序博客网 时间:2024/06/05 13:59

1.导入jquery的js包

2.jsp代码

<%--  Created by IntelliJ IDEA.  User: qqg  Date: 2017/10/11  Time: 10:31  To change this template use File | Settings | File Templates.--%><%@ page language="java" contentType="text/html; charset=utf-8"         pageEncoding="utf-8"%><%    String path = request.getContextPath();    HttpSession s = request.getSession();    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><html><head>    <title>Title</title>    <script type="text/javascript" src="/js/jquery.min.js"></script>    <%--<link href="/css/bootstrap.min.css" rel="stylesheet">    <script src="/js/bootstrap.js"></script>--%></head><body>    <script>        /**         * 获取当前时间         */        function p(s) {            return s < 10 ? '0' + s: s;        }        var myDate = new Date();        //获取当前年        var year=myDate.getFullYear();        //获取当前月        var month=myDate.getMonth()+1;        //获取当前日        var date=myDate.getDate();        var h=myDate.getHours();       //获取当前小时数(0-23)        var m=myDate.getMinutes();     //获取当前分钟数(0-59)        var s=myDate.getSeconds();        var now=year+'-'+p(month)+"-"+p(date)+" "+p(h)+':'+p(m)+":"+p(s);        var now1=year+'-'+p(month);        function alerts() {            alert(now1);        }       // 如果需要月份加1的话        var myDate2 = new Date();        myDate2.setMonth(month + 1);        //获取当前年        var year2=myDate2.getFullYear();        //获取当前月        var month2=myDate2.getMonth();        //获取当前日        var date2=myDate2.getDate();        //根据时间获取当前月的周数,每周的周日为一周的开始        function getWeekByMonth(date) {            var y = date.getFullYear();            var m = date.getMonth()+1;//月份 0 - 11 ,0代表 1 月            var d = date.getDate();            var now2 =y+'-'+p(m)+"-"+p(d);            //获取本月1号对应的周数            var monOneWeek = new Date(y,m-1,1);            var week = monOneWeek.getDay();            //获取本月的天数            var monOneDay = new Date(y,m,1);            var date_count =   (new Date(monOneDay.getTime()-1000*60*60*24)).getDate();            var days = date_count;            var mod = 7 - week;            var count = 1;            var start = 1;  //起始日期 1 号开始            var s = "" , ss = "";            var end = start + mod - 1;//截止日期            while (days >= 0){                //var end = start + 6;                end = end > date_count ? date_count : end ;                s += "第" + count + "周:" +y+"年"+p(m)+"月"+"("+p(start)+"-"+p(end)+");"+"\n";                start = end + 1;                end += 7;                days -= 7;                ss += days + ",";                count ++;            }            return s;        }        function getData() {            //alert(p(m)+"周:"+week+"天数:"+date_count);            //测试月份            //2017年2月1日            var date2 = new Date(2017,1,1);            //2017年1月1日            var date1 = new Date(2017,0,1);            //2017年3月1日            var date3 = new Date(2017,2,1);            //2017年10月1日            var date10 = new Date(2017,9,1);            //2017年11月1日            var date5 = new Date(2017,10,1);            alert(getWeekByMonth(date5));        }    </script>    <div ><button class="btn btn-success" onclick="getData();">测试</button> </div></body></html>

3.效果

这里写图片描述

4.总结

不知道有没有什么简单的方法直接获取,反正就自己写了方法硬硬是算了出来。

阅读全文
0 0