如何将maxDate设置为当年12月的最后一天? [英] How to set maxDate is last day of december on current year?

查看:101
本文介绍了如何将maxDate设置为当年12月的最后一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个日期范围datepicker:

I have this date range datepicker :

$(function () {
  //date picker range 
  $(function () {         
    var dateFormat = "mm/dd/yy",
      from = $("#<%= TextBox1.ClientID %>").datepicker({
        //defaultDate: "+1w",
        changeMonth: true,
        minDate: MinDateManipulation(),
        beforeShowDay: DisableMonday,
        maxDate: '+2M',
        numberOfMonths:1
      })
      .on("change", function () {
        to.datepicker("option", "minDate", getDate(this));
      }),
      to = $("#<%= TextBox2.ClientID %>").datepicker({
        //defaultDate: "+1w",
        changeMonth: true,
        beforeShowDay: DisableMonday,
        maxDate: '+2M',
        numberOfMonths:1
      })
      .on("change", function () {
        from.datepicker("option", "maxDate", getDate(this));
      });
    function getDate(element) {
      var date;
      try {
        date = $.datepicker.parseDate(dateFormat, element.value);
      } catch (error) {
        date = null;
      }
      return date;
    }
  });

当我尝试从当前日期后的2个月起禁用日期时,它起作用了,但目的是在当年之后的下一年的每年1月1日禁用日期.

It work when i tried to disable date from 2 months after current date, but the purpose is, to disable date in January 1st every next year after current year.

例如:当前日期[11/3/2018]

for example : current date [11/3/2018]

我想使用maxDate [31/12/2018]禁用每个年度的12月最后一天之后选择的日期.

I want to disable date to chose after last day of December every current year, with maxDate [31/12/2018].

但是我们如何动态设置maxDate的值?无需使用daterange条件脚本手动更新年份的值?

But how do we set dynamically for the value of the maxDate? without update the value of the year manually with my daterange condition script?

推荐答案

目前尚不清楚您要捕获的范围,但是您可以利用已有的资源做很多事情.

It's not clear the range you are trying to capture, yet you can do a lot with what you already have.

一种方法是将其设置为字符串格式:

One way would be to set it to a string format:

var yr = $.datepicker.formatDate("yy", new Date());
var dec31 = "12/31/" + yr;
....({
  maxDate: dec31,
})

这将得到当前的Year(2018)并创建一个可用作最大日期的字符串:12/31/2018.这将保持动态并每年更新.

this will get the current Year (2018) and create a string that can be used as the max date: 12/31/2018. This will remain dynamic and update each year.

您还可以将一年中的天数定义为假期.您可以做很多事情.

You could also define a number of days in the year as holidays. Lots of things you can do.

也许此示例将帮助您清除一些内容.

Maybe this example will help clear up a few things.

$(function() {
  var holidays = {
    2018: {
      11: {
        12: [
          false,
          "holiday",
          "Veterans Day Observed"
        ],
        22: [
          false,
          "holiday",
          "Thanksgiving Day"
        ]
      },
      12: {
        25: [
          false,
          "holiday",
          "Christmas Day"
        ],
        31: [
          false,
          "holiday",
          "New Year's Eve"
        ]
      }
    },
    2019: {
      1: {
        1: [
          false,
          "holiday",
          "New Year's Day"
        ],
        21: [
          false,
          "holiday",
          "Martin Luther King Jr. Day"
        ]
      },
      2: {
        18: [
          false,
          "holiday",
          "Presidents' Day"
        ]
      },
      5: {
        27: [
          false,
          "holiday",
          "Memorial Day"
        ]
      },
      7: {
        4: [
          false,
          "holiday",
          "Independence Day"
        ]
      },
      9: {
        2: [
          false,
          "holiday",
          "Labor Day"
        ]
      },
      10: {
        14: [
          false,
          "holiday",
          "Columbus Day"
        ]
      },
      11: {
        11: [
          false,
          "holiday",
          "Veterans Day"
        ],
        28: [
          false,
          "holiday",
          "Thanksgiving Day"
        ]
      },
      12: {
        25: [
          false,
          "holiday",
          "Christmas Day"
        ],
        31: [
          false,
          "holiday",
          "New Year's Eve"
        ]
      }
    }
  };

  function disableDays(d) {
    var result = [true, ""];
    var yr = $.datepicker.formatDate("yy", d),
      mo = $.datepicker.formatDate("m", d),
      dy = $.datepicker.formatDate("d", d);
    if ($.datepicker.formatDate("D", d) == "Mon") {
      result[0] = false;
    }
    if (holidays[yr] !== undefined) {
      if (holidays[yr][mo] !== undefined) {
        if (holidays[yr][mo][dy] !== undefined) {
          console.log("Holiday:", yr, mo, dy);
          result = holidays[yr][mo][dy];
        }
      }
    }
    return result;
  }

  var dateFormat = "mm/dd/yy",
    from = $("#client-id-1").datepicker({
      //defaultDate: "+1w",
      changeMonth: true,
      minDate: 0,
      beforeShowDay: disableDays,
      maxDate: '+1y',
      numberOfMonths: 1
    })
    .on("change", function() {
      to.datepicker("option", "minDate", getDate(this));
    }),
    to = $("#client-id-2").datepicker({
      //defaultDate: "+1w",
      changeMonth: true,
      beforeShowDay: disableDays,
      maxDate: '+2m',
      numberOfMonths: 1
    })
    .on("change", function() {
      from.datepicker("option", "maxDate", getDate(this));
    });

  function getDate(element) {
    var date;
    try {
      date = $.datepicker.parseDate(dateFormat, element.value);
    } catch (error) {
      date = null;
    }
    return date;
  }
});

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Client 1: <input type="text" id="client-id-1"></p>
<p>Client 2: <input type="text" id="client-id-2"></p>

这篇关于如何将maxDate设置为当年12月的最后一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆