jQuery datepicker getMinDate'+ 1d' [英] jQuery datepicker getMinDate '+1d'

查看:172
本文介绍了jQuery datepicker getMinDate'+ 1d'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦我设置了一个具有方便的字符串语法的datepicker的minDate属性

Once I have set the minDate property of a datepicker with the convenient string syntax

$(elem).datepicker('option','minDate','+1d +3m'); 

如何获取minDate的日期对象?为了帮助说明,有一种方法

how can I get the date object of the minDate? To help illustrate, there is a method

 $(elem).datepicker('getDate');

它以日期对象的格式返回在输入中输入的日期。我想要同样的事情,但对于datepicker('getMinDate')。有一个这样的选项

which returns the date that is entered in the input in the format of a date object. I would like the same thing but for datepicker('getMinDate'). There is an option like this

$(elem).datepicker('option','minDate');

但是这会返回'+ 1d + 3m',这是没有帮助的。我需要实际的日期对象与另一个日期对象进行比较。任何想法?

but this returns '+1d +3m' which is not helpful. I need the actual date object to compare with another date object. Any ideas?

推荐答案

jQuery使用其 _determineDate()函数来计算minDate date对象基于其属性。我修改了它的行为并发挥了作用。请注意,它只处理偏移类型的值,没有其他的。

jQuery uses its _determineDate() function to calculate the minDate date object based on its attribute. I modified its behaviour and made a function. Note that it only deals with the "offset" type of values and nothing else.

/* minDateAttr is the minDate option of the datepicker, eg '+1d +3m' */
function getMinDate(minDateAttr) {
    var minDate = new Date();
    var pattern = /([+-]?[0-9]+)\s*(d|D|w|W|m|M|y|Y)?/g;
    var matches = pattern.exec(minDateAttr);
    while (matches) {
        switch (matches[2] || 'd') {
            case 'd' : case 'D' :
                minDate.setDate(minDate.getDate() + parseInt(matches[1],10));
                break;
            case 'w' : case 'W' :
                minDate.setDate(minDate.getDate() + parseInt(matches[1],10) * 7);
                break;
            case 'm' : case 'M' :
                minDate.setMonth(minDate.getMonth() + parseInt(matches[1],10));
                break;
            case 'y': case 'Y' :
                minDate.setYear(minDate.getFullYear() + parseInt(matches[1],10));
                break;
        }
        matches = pattern.exec(minDateAttr);
    }
    return minDate;
}



我原来计划在回答以下,但是提出了一个(更好的)解决方案 - 上面的一个。但是,我要包括它,以防需要调试原因等。

_determineDate() code>函数在技术上是可用的,但它不应该被使用,并可能在将来改变。然而,这将是如何使用它:

The _determineDate() function is technically availible for use, but it's not supposed to be used and may change in the future. Nevertheless, this would be how to use it:

var minDateAttr = $(elem).datepicker("option", "minDate");
var inst = $(elem).data("datepicker");
var minDateObj = $.datepicker._determineDate(inst, minDateAttr, new Date());

这篇关于jQuery datepicker getMinDate'+ 1d'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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