有没有办法让jQuery datepicker无需多年工作?

时间:2013-11-21 18:33:13

标签: javascript jquery jquery-ui date datepicker

我想使用datepicker作为“历史上的这一天”项目的一部分。

这个想法是用户选择一天,比如“11月20日”,该项目向他展示了当天发生的各种历史事件。

鉴于他们选择的是通用日,而非特定日期(如“2013年11月20日”),我希望日期选择器与年份无关。

Several people have asked类似的东西,但他们正在寻找 隐藏 年份的方法。

我不想隐藏这一年。我希望选择器 忽略 年份。

应该没有日期标签(“星期一”,“星期二”等),并且在第一周开始时没有空白日期。在二月的情况下,它将列出29天。

所以,而不是像这样......

NOVEMBER
Mo  Tu  We  Th  Fr  Sa  Su
--  --  --  1   2   3   4
5   6   7   8   9   10  11
12  13  14  15  16  17  18

......就像这样

NOVEMBER
1   2   3   4   5   6   7
8   9   10  11  12  13  14
15  16  17  18  19  20  21

有没有办法修改jQuery UI的datepicker才能像这样工作?或者是一个不同的库,为他们的日期选择器提供一年的选项?

1 个答案:

答案 0 :(得分:2)

您可以通过覆盖Date.prototype.getDay()方法来实现此目的:

JS:

$(function() {
    //override getDay()
    Date.prototype.getDay = function() {
        //change getDay() so that the first day of the month is always at 
        // the beginning of the week
        return ((this.getDate()-1) % 7);
    }

    //create the datepicker that does not display year
    $( "#datepicker" ).datepicker({dateFormat: "d MM"});
});

CSS(隐藏日期选择器中的年份和日期):

.ui-datepicker-year, .ui-datepicker-calendar thead {
    display: none;
}

Fiddle

相关问题