0

我用新日期创建的字符串日期更新了我的代码,并添加回了 if 语句。不过,这并没有禁用字符串或范围。我也添加了日期选择器代码。

function unavailableDays(date) {

function createDateRange(first, last) {
    var dates = [];
    for(var j = first; j < last; j.setDate(j.getDate() + 7)) {
        dates.push(new Date(j.getTime()));
    }
    var alwaysDisabled = [new Date("1963-3-10T00:00:00"), new Date("1963-3-17T00:00:00"), new Date("1963-3-24T00:00:00"), new Date("1963-3-31T00:00:00"), new Date("1965-9-18T00:00:00")];
    return dates.concat(alwaysDisabled);
}

var disabledDays = createDateRange(new Date("1978-8-10T00:00:00"), new Date("1978-11-5T00:00:00"));

var yy = date.getFullYear(), mm  = date.getMonth(), dd = date.getDate();
    for (i = 0; i < disabledDays.length; i++) {
    if($.inArray(yy + '-' + (mm+1) + '-' + dd,disabledDays) != -1 || new Date() < date) {
        return [false];
    }
}
    return [true];
}

$(document).ready(function (){
$('.selector').datepicker({
    inline: true,
    dateFormat: 'yy-mm-dd',
    constrainInput: true,
    changeYear: true,
    changeMonth: true,
    minDate: new Date(1940, 1-1, 1),
    maxDate: new Date(2011, 10-1, 24),
    beforeShowDay: unavailableDays,    
    onSelect: function(dateText, inst) {
            $("#img").attr("src", "http://www.example.com" + dateText + ".jpg"); 
         var chosenDates = $.datepicker.parseDate('yy-mm-dd', dateText);
         var backToString = $.datepicker.formatDate('MM dd' + ',' + ' yy', chosenDates);
         $('.info').html('You are viewing:' + '<br />' +
             backToString).addClass('background'); 
    } 
});

});

4

3 回答 3

3

In your function, dates.push needs to be date.push.

于 2012-04-09T01:52:20.213 回答
1

因此,您似乎只想分离出功能:

function createDateRange(first, last) {
    var date = [];
    for(var j = first; j < last; j.setDate(j.getDate() + 7))
        dates.push(new Date(j.getTime()));
    return date;
}

function unavailableDays(date) {
    var disabledDays = createDateRange(new Date("1978-08-10"), new Date("1978-11-05"));

    //date array to be disabled
    var disabledDays = ["1963-3-10", "1963-3-17", "1963-3-24", "1963-3-31", "1965-9-18"];
    var yy = date.getFullYear(), mm = date.getMonth(), dd = date.getDate();
    for (i = 0; i < disabledDays.length; i++) {
        if($.inArray(yy + '-' + (mm+1) + '-' + dd,disabledDays) != -1 || new Date() < date) {
            return [false];
        } 
    }
    return [true]; 
}

这将允许您在另一个函数中使用createDateRange和:unavailableDays

function someOtherFunction() {
    var someDateRange = createDateRange(new Date('1979-10-10'), new Date('1980-01-01'));
    … // stuff
}
于 2012-04-09T01:49:47.937 回答
0

在另一个函数内部拥有一个函数没有问题,JavaScript 几乎就是基于此。你也可以有单独的功能,它也可以工作。

更新

(考虑到datevsdates问题是固定的)

Here is what seems to be the problem (this is what your question seems to be really about, not functions inside of functions!): createDateRange returns an array of Date objects, but your other version (var disabledDays = ["1963-3-10", ...) is an array of strings (and in a format that cannot be parsed by new Date(str)).

Your current loop seems to be trying to deal with the this string version, and it works, but you want to createDateRange to always ignore certain dates (as far as I understand what you are saying). So, try this:

function unavailableDays(date) {

    function createDateRange(first, last) {
        var dates = [];
        for(var j = first; j < last; j.setDate(j.getDate() + 7)) {
            dates.push(new Date(j.getTime()));
        }
        var alwaysDisabled = [new Date("1963-03-10T00:00:00"), new Date("1963-03-17T00:00:00"), new Date("1963-03-24T00:00:00"), new Date("1963-03-31T00:00:00"), new Date("1965-09-18T00:00:00")];
        return dates.concat(alwaysDisabled);
    }

    var disabledDays = createDateRange(new Date("1978-08-10T00:00:00"), new Date("1978-11-05T00:00:00"));
    for (i = 0; i < disabledDays.length; i++) {
        if(disabledDays[i].getTime() == date.getTime()) {
            return false;
        }
    }
    return true;

}

// Use `T00:00:00` after the date to set it as GMT, or
// dates can be interpreted as the previous day on some timezones.
var testDate1 = new Date('1978-08-10T00:00:00')
console.log("-- " + unavailableDays(testDate1)); // false
var testDate2 = new Date('1978-08-11T00:00:00')
console.log("-- " + unavailableDays(testDate2)); // true
var testDate3 = new Date('1978-08-17T00:00:00')
console.log("-- " + unavailableDays(testDate3)); // false
var testDate4 = new Date('1963-03-10T00:00:00')
console.log("-- " + unavailableDays(testDate4)); // false

http://jsfiddle.net/t4ahF/4/

于 2012-04-09T01:51:58.857 回答