jquery datetimepicker设置为日期depand on date

时间:2015-05-29 07:21:31

标签: javascript php jquery datetime datepicker

我正在使用jQuery" datetimepicker"我想将"设置为日期"价值取决于"从日期"值和包含以下值的“选择”框值。

1-周(7天以上)
2-每月(30天以上)
半年半(6个月以上)
3年(1年以上)

示例:

1-选择日期:2015-05-29
2-持续时间每月
3-迄今为止应该是2015-06-29

我使用以下代码选择日期开始日期。

jQuery('#start_date').datetimepicker({
    format:'m/d/Y',
    closeOnDateSelect:true,
    timepicker:false
});

请建议。

由于

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你会有这样的事情:

a)像这样的选择框

Options : 
<select id="time">
    <option value="1">Weekly (7+ days)</option>
    <option value="2">Monthly (30+ days)</option>
    <option value="3">Half Yearly (6+ months)</option>
    <option value="4">Yearly (1+ Year)</option>
</select>

b)和两个日期选择器,如下:

Select From Date :
<input id="start_date" type="text" />
Select END Date :
<input id="end_date" type="text" />

c)日期选择器和onSelect函数的代码将更改第二个日期选择器:

/** addExtraTime() function 
*    this function changes the second datepicker ( $('#end_date').datepicker )
*    according to the selected value of select box.
*/
var addExtraTime = function (aDateObj) {
    var actualDate = aDateObj;
    var newDate = aDateObj;
    var extraTime = $('#time').val(); //string
    if (extraTime === '1') { //Weekly = +7d
        newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate() + 7);
        $('#end_date').datepicker('setDate', newDate);
    } else if (extraTime === '2') { //Monthly = +1m
        newDate = new Date(actualDate.getFullYear(), actualDate.getMonth() + 1, actualDate.getDate());
        $('#end_date').datepicker('setDate', newDate);
    } else if (extraTime === '3') { //Half Yearly = +6m
        newDate = new Date(actualDate.getFullYear(), actualDate.getMonth() + 6, actualDate.getDate());
        $('#end_date').datepicker('setDate', newDate);
    } else if (extraTime === '4') { //Yearly = +1y
        newDate = new Date(actualDate.getFullYear() + 1, actualDate.getMonth(), actualDate.getDate());
        $('#end_date').datepicker('setDate', newDate);
    } //End of if..else
};

/* We watch for changes in the select box and call the addExtraTime() */
$('#time').change(function () {
    var currentDate = $('#start_date').datepicker("getDate");
    addExtraTime(currentDate);
});

/* From Date picker */
$('#start_date').datepicker({
    format: 'm/d/Y',
    closeOnDateSelect: true,
    timepicker: false,
    onSelect: function (selectedDate) {
        /*
         * selectedDate is a string so we convert is to a Date obj
         */
        var selectedDateObj = new Date(selectedDate);
        addExtraTime(selectedDateObj);
    } //End of onSelect

});

/* To Date picker */
$('#end_date').datepicker({
    format: 'm/d/Y',
    closeOnDateSelect: true,
    timepicker: false
});

您可以看到此操作:here