通过某日期得到该日期所在的一周的周一和周日的日期

function textGetDateTime(e) {
            var datetime = e.value;//传入的某日期
            //var date = new Date(datetime);//带参数的Date方法在IE浏览器中无法正常使用,因此用setUTCFullYear(year,month,day)方法来替换,该方法是设置时间的年-月-日部分,其中月和日为可选
var date = NewDate(datetime);//调用下面的函数
            var weekDay = date.getDay(); //得到一周中的第几天(0~6)
            if (weekDay == 0)
                weekDay = 7;//将周日换算为7

            var monthDay = date.getDate(); //得到一月中的第几天(1~31)

            var startMonthDay = monthDay - weekDay + 1; //得到选择日期对应的某一周的周一是这个月的第几天
            var stopMonthDay = monthDay + 7 - weekDay; //得到选择日期对应的某一周的周日是这个月的第几天

            var startDate = new Date(datetime), stopDate = new Date(datetime);
            startDate.setFullYear(date.getFullYear()); //年份
            stopDate.setFullYear(date.getFullYear());

            startDate.setMonth(date.getMonth()); //月份
            stopDate.setMonth(date.getMonth());

            startDate.setDate(startMonthDay); //一个月中的第几天
            stopDate.setDate(stopMonthDay);

            var newStartMonth = startDate.getMonth()
            , newStopMonth = stopDate.getMonth()
            , newStartDate = startDate.getDate()
            , newStopDate = stopDate.getDate();//得到月份和日期

            //判断月份和号数是否为单,如果为单就添加个0在前面
            if (startDate.getMonth() + 1 < 10)
                newStartMonth = "0" + (startDate.getMonth() + 1);
            if (stopDate.getMonth() + 1 < 10)
                newStopMonth = "0" + (stopDate.getMonth() + 1);
            if (startDate.getDate() < 10)
                newStartDate = "0" + startDate.getDate();
            if (stopDate.getDate() < 10)
                newStopDate = "0" + stopDate.getDate();


            var newStartDate = startDate.getFullYear() + "-" + newStartMonth + "-" + newStartDate;
            var newStopDate = stopDate.getFullYear() + "-" + newStopMonth + "-" + newStopDate;
        }
function NewDate(str) {
            str = str.split('-');
            var date = new Date();
            date.setUTCFullYear(str[0], str[1] - 1, str[2]);//设置年-月-日,其中月,日为可选
            date.setUTCHours(0, 0, 0, 0);//设置时,分,秒,毫秒,其中分,秒,毫秒为可选
            return date;
        } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值