简体   繁体   English

如何伪造jquery-ui-datepicker的当前日期

[英]how to fake current date for jquery-ui-datepicker

I'm implementing a custom algorithm to disable dates depending on a set of rules that also depend on what the current day is. 我正在实现一个自定义算法来禁用日期,具体取决于一组规则,这些规则也取决于当前的日期。 To test that I would need to set the current date to a specific date. 要测试我需要将当前日期设置为特定日期。

I found this thread that just overwrites javascripts Date function. 我发现这个线程只是覆盖了javascripts Date函数。 My problem is that if I do that the calendar doesn't work as expected anymore. 我的问题是,如果我这样做,日历将不再按预期工作。 Most of the time all days are just missing, sometimes all are disabled. 大多数时间都是失踪的,有时候全部都是残疾人。

var oldDate = Date;
Date = function (fake)
{
    if( ! fake ) return new oldDate('02/26/2017');

    return new oldDate(fake);
}
Date.prototype = oldDate.prototype;

How can I test the datepicker with a custom current date? 如何使用自定义当前日期测试日期选择器? It should be as easy as setting up a jsfiddle (if possible): My current Fiddle 它应该像设置一个jsfiddle一样容易(如果可能的话): 我现在的小提琴

I'm not so sure about what you need to do, I usually change operating system time, anyway, plain an simple: 我不太确定你需要做什么,我通常会改变操作系统时间,无论如何,简单来说:

 // DEMO mock date js browser // comment uncoment to play arround, if you need to automate with or without mock date plain an simple: mockDateTime('2018/02/02'); // with mock // mockDateTime() // no mock $('#date1').datepicker({ changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: "m/d/yy" }); function mockDateTime(mockDate){ // to do: validate mock date if (mockDate !== undefined && mockDate.length > 0) { var __Date = Date; Date = undefined; Date = function() { // to do: more deep checks on arguments if (arguments.length > 0) { return new __Date(arguments[0], arguments[1], arguments[2]); } return new __Date(mockDate); }; Date.prototype = __Date.prototype; } } 
 #ui-datepicker-div { font-size: 12px; } 
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> Date Picker on input field: <input type="text" id="date1" name="date1"/> <br/> 

Set this property and try hope this one hep you. 设置这个属性,并尝试希望这一个你。

gotoCurrent gotoCurrent

Type: Boolean 类型: Boolean

Default: false 默认值: false

When true, the current day link moves to the currently selected date instead of today. 如果为true,则当前日期链接将移至当前所选日期而非今天。 Code examples: Initialize the datepicker with the gotoCurrent option specified: 代码示例:使用指定的gotoCurrent选项初始化datepicker:

$( ".selector" ).datepicker({
  gotoCurrent: true
});

Instead of trying to fool the DatePicker I would do it in logic. 而不是试图欺骗DatePicker我会在逻辑中做到这一点。 That is, If I understand your question correctly. 也就是说,如果我正确理解你的问题。 Consider the following design: 考虑以下设计:

  1. You have a function, that accepts a date input, and based on that runs your algorithm which calculates invalid dates based on the input date. 你有一个接受日期输入的函数,并根据它运行你的算法,根据输入日期计算无效日期。 Be that real of fake date, doesn't matter works anyway, assuming your invalid date calculator algorithm works. 假设您的无效日期计算器算法有效,那就是假日期的真实,无论如何都无关紧要。

  2. Using this algo, you can always have an array of invalid dates based on the real or fake current date. 使用此算法,您可以始终根据实际或假的当前日期生成一系列无效日期。

  3. Giving that array to DatePicker, you can define what dates to disable. 将该数组提供给DatePicker,您可以定义要禁用的日期。 (In the beforeShowDay .) (在beforeShowDay 。)

Works well, does not need DatePicker hack. 效果很好,不需要DatePicker hack。 See example implementation: 参见示例实现:

function calculateInvalidDatesBasedOnInputDate(inputDate) {
    // your algorithm
    // fake result here, to demonstrate how it works.
    return ["2/14/2017","2/19/2017","2/28/2017"];
}

var fakeCurrentDate = new Date("2017/02/02");

$('input').datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: "m/d/yy",
    defaultDate: fakeCurrentDate,
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('m/d/yy', date);
        return [ calculateInvalidDatesBasedOnInputDate(fakeCurrentDate).indexOf(string) == -1 ]
    }
});

See Fiddle here . 在这里看小提琴。

Might want to look at $.datepicker.parseDate( format, value, options ) feature. 可能要查看$.datepicker.parseDate( format, value, options )功能。

A number of exceptions may be thrown: 可能会抛出许多异常:

  • 'Invalid arguments' if either format or value is null 如果格式或值为null,则“参数无效”
  • 'Missing number at position nn' if format indicated a numeric value that is not then found '如果格式表示未找到的数值,则在位置nn处缺少数字'
  • 'Unknown name at position nn' if format indicated day or month name that is not then found '如果格式显示未找到的日期或月份名称,则在位置nn处显示未知名称
  • 'Unexpected literal at position nn' if format indicated a literal value that is not then found '如果格式表示未找到的文字值,则在位置nn处出现意外的文字
  • 'Invalid date' if the date is invalid, such as '31/02/2007' 如果日期无效,则“日期无效”,例如'31 / 02/2007'

This is a way to check that you have a valid date and can return a date object built off the date value you've supplied. 这是一种检查您是否具有有效日期的方法,并且可以返回根据您提供的日期值构建的日期对象。 You can then use this in your script. 然后,您可以在脚本中使用它。

A faster way is simply to set your computer (or a test VM) Date & Time to a date and time in the future or past. 更快捷的方法是将您的计算机(或测试VM)日期和时间设置为将来或过去的日期和时间。 This will be past to the browser and then functions like now() or date() will give you much different results. 这将过去浏览器,然后像now()date()这样的函数会给你很多不同的结果。

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

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