js求指定时间的周一和周日

/*计算指定时间的的周一和周日

            return=>{mondy:Date,sundy:Date}

            parms:{

            date:指定时间,如果不指定则取当前时间

            }

            */

            function getWeekArea(date) {

                if (!(date instanceof Date)) {

                    date = new Date();

                }

                var day = date.getDay(), _date = date.getDate(),

                    mondy = _date - (day == 0 ? 7 : day) + 1,

                    t1 = new Date(date.getTime()),

                    t2 = new Date(date.getTime());

                t1.setDate(mondy);

                t2.setDate(mondy + 6);

                return { mondy: t1, sundy: t2 };

            }

            function fnSetWeek(week) {

                var weekArea = getWeekArea(), mondy = weekArea.mondy, sundy = weekArea.sundy, week = parseInt(week, 10);

                if (isNaN(week)) {

                    /*周一查上周的数据*/

                    var t = new Date(), day = t.getDay();

                    return fnSetWeek(day == 1 ? -1 : 0);

                } else {

                    mondy.setDate(mondy.getDate() + week * 7);

                    sundy.setDate(sundy.getDate() + week * 7);

                }

                startTime.val(mondy.formatDate(dateFormat));

                endTime.val(sundy.formatDate(dateFormat));

            }

 

你可能感兴趣的:(js)