在显示之前在 jQuery UI datepicker 中将另一个月设置为默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4170880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:47:00  来源:igfitidea点击:

Setting another month as default in jQuery UI datepicker before display

jqueryjquery-uidatepicker

提问by qinHaiXiang

In my situation, I have to set the month to last month as default.

在我的情况下,我必须将月份设置为上个月作为默认值。

The default month for the datepicker is current month. But I want it to be last month or other month as default showing. How can I make it?

日期选择器的默认月份是当前月份。但我希望它是上个月或其他月份作为默认显示。我怎样才能做到?

Such as: "2010-09" as the default.

如:“2010-09”为默认值。

Thank you very much!

非常感谢!

回答by Yi Jiang

You can use the defaultDateoption when creating the datepicker:

您可以defaultDate在创建日期选择器时使用该选项:

$('#date').datepicker({
    defaultDate: '-2m'
});

By passing in a string like this we can set the default date to another one relative to the current date. Alternatively, the option also accepts a Dateobject:

通过传入这样的字符串,我们可以将默认日期设置为相对于当前日期的另一个日期。或者,该选项也接受一个Date对象:

defaultDate: new Date(2010, 8, 1)

or a string in the same format as the format currently defined:

或与当前定义的格式相同格式的字符串:

defaultDate: '1/9/2010'

All of the above will give you a default date in September. The month in the Dateconstructor starts from zero, so 8 will give you September.

以上所有内容都会为您提供一个默认日期为 9 月。Date构造函数中的月份从零开始,因此 8 将为您提供九月。

回答by toxaq

I think you actually want this when you're only showing 1 month at a time.

我认为当您一次只展示 1 个月时,您实际上想要这个。

$( ".selector" ).datepicker({ showCurrentAtPos: 1 });

If you use defaultDate it will highlight that particular day, this method just shows the previous month with nothing selected.

如果您使用 defaultDate 它将突出显示该特定日期,此方法仅显示上个月而未选择任何内容。

回答by Null Pointer

Try this

尝试这个

$( ".selector" ).datepicker({ defaultDate: +7 });

Specify either an actual date via a Date object or as a string in the current dateFormat, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null for today.

通过 Date 对象或当前 dateFormat 中的字符串指定实际日期,或从今天起的天数(例如 +7)或一串值和句点('y' 表示年,'m' 表示月, 'w' 表示周,'d' 表示天,例如 '+1m +7d'),或者今天为 null。