Time control linkage, according to the selection date +7 days

When the page is initialized, obtain the current time, set the start time, end time of maximum, minimum.
When the user selects the start time, trigger change event, access to user-selected time +7, set the end time of the maximum, minimum

FormatDate which method is the use of other bloggers but I have forgotten who is in the up. . . sorry. . . Please contact me to see if I was to add a jump link

Off-topic: I use the time controls are alone in the end the most important micro-channel mobile browser did not achieve the desired results (Manual smile)

The complete code is as follows

    var minday = new Date();	//获取当前时间
    var maxday = new Date(minday.getTime()+604800000);  //毫秒计算:7*24*60*60*1000

    minday = FormatDate(minday.getTime());
    maxday = FormatDate(maxday.getTime());
    //设置开始时间最小值为当日,最大值为当日+7
    $("#startTime").attr("min",minday);
    $("#startTime").attr("max",maxday);
    //设置结束时间最小值为当日,最大值为当日+7
    $("#endTime").attr("min",minday);
    $("#endTime").attr("max",maxday);

    //开始时间状态改变时间
    $("#startTime").change(function(){
        var startday=new Date($(this).val());            //获取用户选择的开始时间
        startday.setDate(startday.getDate()+7);             //开始时间+7天
        startday=FormatDate(startday);                      //时间转换为YYYY-MM-DD格式

        $("#endTime").attr("max",startday); //最大时间为开始时间+7天
        $("#endTime").attr("min",$(this).val()); //最小时间为用户选择的开始时间
    });
    
    //转换时间类型为 yyyy-mm-dd
    function FormatDate (strTime) {
        var date = new Date(strTime);
        var formatedMonth = ("0" + (date.getMonth() + 1)).slice(-2);
        var formatedDate = ("0" + (date.getDate())).slice(-2);
        return date.getFullYear()+"-"+formatedMonth+"-"+formatedDate;
    }

Guess you like

Origin blog.csdn.net/User_Lily/article/details/92972166