inArray或indexof在日期数组中查找值

时间:2015-03-02 18:39:47

标签: javascript jquery

$(function () {
    var arDates = [ new Date("2014-03-15").valueOf(),
    new Date("2014-03-19").valueOf(),
    new Date("2014-03-21").valueOf(),
    new Date("2014-03-24").valueOf(),
    new Date("2014-03-26").valueOf(),
    new Date("2014-03-28").valueOf(),
    new Date("2014-03-31").valueOf(),
    new Date("2014-04-03").valueOf(),
    new Date("2014-04-06").valueOf(),
    new Date("2014-04-08").valueOf(),
    new Date("2014-04-15").valueOf(),
    new Date("2014-04-22").valueOf(),
    new Date("2014-04-24").valueOf(),
    new Date("2014-04-27").valueOf(),
    new Date("2014-04-30").valueOf(),
    new Date("2014-05-03").valueOf(),
    new Date("2014-05-06").valueOf(),
    new Date("2014-05-08").valueOf(),
    new Date("2014-05-15").valueOf(),
]
    $('.date').pickmeup({
        date        : new Date(1397084400000),
        flat        : true,
        format      : 'Y-m-d',
        calendars   : 3,
        render: function(date) {
            if ($.inArray(date.valueOf(), arDates) != -1)
            {
                return {
                    class_name: 'cal_log_date'                         
                }
            }
        }
    });
})

我正在尝试创建一个函数来检查日期是否在数组中,如果是,它会执行某些操作。但它似乎不适用于2014-03-30之后的任何日期,我不明白为什么?

下面是运行代码的示例:http://jsfiddle.net/L2aq4ph2/

1 个答案:

答案 0 :(得分:1)

您的问题是夏令时:

Sun Mar 30 2014 00:00:00 GMT+0000 (GMT Standard Time)

Mon Mar 31 2014 00:00:00 GMT+0100 (GMT Daylight Time)

你必须一直使用UTC times ......

$(function () {
var arDates = [ 
    Date.UTC(2014, 3-1, 15),
    Date.UTC(2014, 3-1, 19),
    Date.UTC(2014, 3-1, 21),
    Date.UTC(2014, 3-1, 24),
    Date.UTC(2014, 3-1, 26),
    Date.UTC(2014, 3-1, 28),
    Date.UTC(2014, 3-1, 31),
    Date.UTC(2014, 4-1, 03),
    Date.UTC(2014, 4-1, 06),
    Date.UTC(2014, 4-1, 08),
    Date.UTC(2014, 4-1, 15),
    Date.UTC(2014, 4-1, 22),
    Date.UTC(2014, 4-1, 24),
    Date.UTC(2014, 4-1, 27),
    Date.UTC(2014, 4-1, 30),
    Date.UTC(2014, 5-1, 03),
    Date.UTC(2014, 5-1, 06),
    Date.UTC(2014, 5-1, 08),
    Date.UTC(2014, 5-1, 15)
]
$('.date').pickmeup({
    date        : new Date(1397084400000),
    flat        : true,
    format      : 'Y-m-d',
    calendars   : 3,
    render: function(date) {

        var y = date.getUTCFullYear();
        var m = date.getUTCMonth();
        var d = date.getUTCDate();

        var evaluateDate = Date.UTC(y, m, d);

        if ($.inArray(evaluateDate, arDates) != -1)
        {
            return {
                class_name: 'cal_log_date'                         
            }
        }
    }
});

DEMO