简体   繁体   English

如何在ASP.Net MVC 5应用程序的JQueryUI DatePicker中设置最小和最大日期

[英]How to set min and max date in the JQueryUI DatePicker in ASP.Net MVC 5 Application

Ok, so after following this Date Picker Tutorial I am able to create a datepicker. 好的,因此在遵循此日期选择器教程之后,我可以创建一个日期选择器。 But I want to set some limit to the date picker. 但是我想对日期选择器设置一些限制。 For example I only want the date picker to allow from 5th Jan 2017 to 5th Aug 2017". How can I achieve this. Note that I don't want to change the dateFormat property of the datepicker. Also, dateformat property is working properly. 例如,我只希望日期选择器在2017年1月5日到2017年8月5日之间允许。“如何实现这一点。请注意,我不想更改dateFormat属性。而且, dateformat属性正常运行。

Below is what I have till now: 以下是我到目前为止所拥有的:

<script type="text/javascript">
    $(function () {
        $(".date-picker").datepicker({
            dateFormat: 'dd-M-yy',
            minDate: '05-Jan-17',
            maxDate: '05-Aug-17'
        });   

    });    
</script>

I selected minDate as '05-Jan-17' because I thought it should be put in the same format as I have set the dateFormat as dd-M-yy . 我将minDate选择为'05 -Jan-17',是因为我认为应将其设置为与将dateFormat设置为dd-M-yy相同的格式。

Documentation of JQuery UI: JQuery UI文档:

在此处输入图片说明

After going through the Documentation of JQUERY DatePicker I thought I need to do something like below. 看完JQUERY DatePicker文档后,我认为我需要做下面的事情。

<script type="text/javascript">
    $(function () {
        $(".date-picker").datepicker({
            dateFormat: 'dd-M-yy',
         });
        $(".date-picker").datepicker("option", "minDate", "05-JAN-17");
    });
</script>

But this also does not work. 但这也不起作用。 Please help me. 请帮我。

Model Class 型号类别

 public class Genre
    {
        public int Id { get; set; }
        [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
        public DateTime SongDate { get; set; }
    }

EDIT 1: This also does not work. 编辑1:这也不起作用。

$(".date-picker").datepicker("option", "minDate", new Date("05-Jan-2017"));

EDIT 2 Below change worked 编辑2以下更改有效

 $(".date-picker").datepicker("option", "minDate", new Date("01-05-2017"));
        $(".date-picker").datepicker("option", "maxDate", new Date("08-05-2017"));

minDate , maxDate should be Date object instead of string, so create a minDatemaxDate应该是Date对象而不是字符串,因此创建一个

new Date(....) in minDate and maxDate accordingly as per your requirement. 根据您的要求, maxDateminDatemaxDate new Date(....)

for reference, read this question 供参考,请阅读此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM