My97DatePicker是一個非常優秀的日曆插件,不僅支持多種調用模式,還支持日期範圍限制。
常規的調用比較簡單,如下所示:
1 | < input class = "Wdate" id = "d1" onclick = "WdatePicker()" /> |
下面重點說明日期範圍限制:
1)靜態限制
你可以給通過配置minDate(最小日期),maxDate(最大日期)爲靜態日期值,來限定日期的範圍
示例1.1:限制日期的範圍是 2012-12-1到2012-12-20
1 | < input class = "Wdate" id = "d2" onfocus = "WdatePicker({skin:'whyGreen',minDate:'2012-12-1',maxDate:'2012-12-20'})" /> |
示例1.2:限制日期的範圍是 2012-12-4 21:30:00 到 2012-12-4 23:59:30
1 | < input type = "text" id = "d3" class = "Wdate" onfocus = "WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'2012-12-4 21:30:00',maxDate:'2012-12-4 23:59:30'})" value = '2012-12-4 21:30:00' /> |
示例1.3:限制日期的範圍是 2012年12月 到 2013年12月
1 | < input type = "text" class = "Wdate" id = "d4" onfocus = "WdatePicker({dateFmt:'yyyy年M月',minDate:'2012-12',maxDate:'2013-12'})" /> |
示例1.4:限制時間的範圍是 9:00:00 到 18:30:00
1 | < input class = "Wdate" id = "d5" onfocus = "WdatePicker({dateFmt:'H:mm:ss',minDate:'9:00:00',maxDate:'18:30:00'})" /> |
2)動態限制
你可以通過系統給出的動態變量,如%y(當前年),%M(當前月)等來限度日期範圍,你還可以通過{}進行表達式運算,如:{%d+1}:表示明天
動態變量表
格式 | 說明 |
---|---|
%y | 當前年 |
%M | 當前月 |
%d | 當前日 |
%ld | 本月最後一天 |
%H | 當前時 |
%m | 當前分 |
%s | 當前秒 |
{} | 運算表達式,如:{%d+1}:表示明天 |
#F{} | {}之間是函數可寫自定義JS代碼 |
示例2.1:只能選擇今天以前的日期(包括今天)
1 | < input id = "d421" class = "Wdate" type = "text" onfocus = "WdatePicker({skin:'whyGreen',maxDate:'%y-%M-%d'})" /> |
示例2.2:使用了運算表達式 只能選擇今天以後的日期(不包括今天)
1 | < input id = "d422" class = "Wdate" type = "text" onfocus = "WdatePicker({minDate:'%y-%M-{%d+1}'})" /> |
示例2.3:只能選擇本月的日期1號至本月最後一天
1 | < input id = "d423" class = "Wdate" type = "text" onfocus = "WdatePicker({minDate:'%y-%M-01',maxDate:'%y-%M-%ld'})" /> |
示例2.4:只能選擇今天7:00:00至明天21:00:00的日期
1 | < input id = "d424" class = "Wdate" type = "text" onfocus = "WdatePicker({dateFmt:'yyyy-M-d H:mm:ss',minDate:'%y-%M-%d 7:00:00',maxDate:'%y-%M-{%d+1} 21:00:00'})" /> |
示例2.5:使用了運算表達式 只能選擇 20小時前 至 30小時後 的日期
1 | < input id = "d425" class = "Wdate" type = "text" onClick = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',minDate:'%y-%M-%d {%H-20}:%m:%s',maxDate:'%y-%M-%d {%H+30}:%m:%s'})" /> |
3)腳本自定義限制
系統提供了$dp.$D和$dp.$DV這兩個API來輔助你進行日期運算,此外你還可以通過在 #F{} 中填入你自定義的腳本,做任何你想做的日期限制
示例3.1: 前面的日期不能大於後面的日期且兩個日期都不能大於 2020-10-01
從 到1 | < input id = "d4311" class = "Wdate" type = "text" onFocus = "WdatePicker({maxDate:'#F{$dp.$D(\'d4312\')||\'2020-10-01\'}'})" /> |
2 | < input id = "d4312" class = "Wdate" type = "text" onFocus = "WdatePicker({minDate:'#F{$dp.$D(\'d4311\')}',maxDate:'2020-10-01'})" /> |
示例3.2:前面的日期+3天 不能大於 後面的日期
從 到1 | < input type = "text" class = "Wdate" id = "d4321" onFocus = "WdatePicker({maxDate:'#F{$dp.$D(\'d4322\',{d:-3});}'})" /> |
2 | < input type = "text" class = "Wdate" id = "d4322" onFocus = "WdatePicker({minDate:'#F{$dp.$D(\'d4321\',{d:3});}'})" /> |
示例3.3:前面的日期+3月零2天 不能大於 後面的日期 且 前面日期都不能大於 2020-4-3減去3月零2天 後面日期 不能大於 2020-4-3
從 到1 | < input type = "text" class = "Wdate" id = "d4331" onFocus = "WdatePicker({maxDate:'#F{$dp.$D(\'d4332\',{M:-3,d:-2})||$dp.$DV(\'2020-4-3\',{M:-3,d:-2})}'})" /> |
2 | < input type = "text" class = "Wdate" id = "d4332" onFocus = "WdatePicker({minDate:'#F{$dp.$D(\'d4331\',{M:3,d:2});}',maxDate:'2020-4-3'})" /> |
示例3.4:發揮你的JS才能,定義任何你想要的日期限制
自動轉到隨機生成的一天,當然,此示例沒有實際的用途,只是爲演示目的
01 | < script > |
02 | //返回一個隨機的日期 |
03 | function randomDate(){ |
04 | var Y = 2000 + Math.round(Math.random() * 10); |
05 | var M = 1 + Math.round(Math.random() * 11); |
06 | var D = 1 + Math.round(Math.random() * 27); |
07 | return Y+'-'+M+'-'+D; |
08 | } |
09 | </ script > |
10 | < input type = "text" class = "Wdate" id = "d434" onFocus = "var date=randomDate();WdatePicker({minDate:date,maxDate:date})" /> |
4)無效天限制
可以使用此功能禁用週日至週六所對應的日期,相關屬性:disabledDays (0至6 分別代表週日至週六)
示例4.1:禁用 週六 所對應的日期
1 | < input id = "d441" type = "text" class = "Wdate" onFocus = "WdatePicker({disabledDays:[6]})" /> |
禁用 週六 週日 所對應的日期
1 | < input id = "d442" type = "text" class = "Wdate" onFocus = "WdatePicker({disabledDays:[0,6]})" /> |
5)無效日期限制
可以使用此功能禁用,所指定的一個或多個日期,只要熟悉正則表達式,可以盡情發揮
用法(正則匹配):
如果你熟悉正則表達式,會很容易理解下面的匹配用法
如果不熟悉,可以參考下面的常用示例
['2008-02-01','2008-02-29'] 表示禁用 2008-02-01 和 2008-02-29
['2008-..-01','2008-02-29'] 表示禁用 2008-所有月份-01 和 2008-02-29
['200[0-8]]-02-01','2008-02-29'] 表示禁用 [2000至2008]-02-01 和 2008-02-29
['^2006'] 表示禁用 2006年的所有日期
此外,您還可以使用 %y %M %d %H %m %s 等變量, 用法同動態日期限制 注意:%ld不能使用
['....-..-01','%y-%M-%d'] 表示禁用 所有年份和所有月份的第一天和今天
['%y-%M-{%d-1}','%y-%M-{%d+1}'] 表示禁用 昨天和明天
當然,除了可以限制日期以外,您還可以限制時間
['....-..-.. 10\:00\:00'] 表示禁用 每天10點 (注意 : 需要 使用 \: )
示例5.1:禁用 每個月份的 5日 15日 25日
1 | < input id = "d451" type = "text" class = "Wdate" onFocus = "WdatePicker({disabledDates:['5$']})" /> |
2 | //注意 :'5$' 表示以 5 結尾 注意 $ 的用法 |
示例5.2:禁用 所有早於2000-01-01的日期
1 | < input id = "d452" type = "text" class = "Wdate" onFocus = "WdatePicker({disabledDates:['^19']})" /> |
2 | //注意:'^19' 表示以 19 開頭 注意 ^ 的用法 |
3 | //當然,可以使用minDate實現類似的功能 這裏主要是 在演示 ^ 的用法 |
示例5.3:配合min/maxDate使用,可以把可選擇的日期分隔成多段
1 | < input id = "d453" type = "text" class = "Wdate" onFocus = "WdatePicker({minDate:'%y-%M-01',maxDate:'%y-%M-%ld',disabledDates:['0[4-7]$','1[1-5]$','2[58]$']})" /> |
示例5.4:min/maxDate disabledDays disabledDates 配合使用 即使在要求非常苛刻的情況下也能滿足需求
1 | < input id = "d454" type = "text" class = "Wdate" onFocus = "WdatePicker({minDate:'%y-%M-01',maxDate:'%y-%M-%ld',disabledDates:['0[4-7]$','1[1-5]$','2[58]$'],disabledDays:[1,3,6]})" /> |
示例5.5:禁用前一個小時和後一個小時內所有時間 使用 %y %M %d %H %m %s 等變量
1 | < input id = "d2a25" type = "text" class = "Wdate" onFocus = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',disabledDates:['%y-%M-%d {%H-1}\:..\:..','%y-%M-%d {%H+1}\:..\:..']})" /> |
示例5.6:#F{}也是可以使用的
本示例利用自定義函數 隨機禁用0-23中的任何一個小時
打開小時選擇框,你會發現有一個小時被禁用的,而且每次禁用的小時都不同
01 | < script > |
02 | function randomH(){ |
03 | //產生一個隨機的數字 0-23 |
04 | var H = Math.round(Math.random() * 23); |
05 | if(H< 10 ) H = '0' +H; |
06 | //返回 '^' + 數字 |
07 | return '^'+H; |
08 | } |
09 | </script> |
10 | < input type = "text" class = "Wdate" id = "d456" onFocus = "WdatePicker({dateFmt:'HH:mm:ss',disabledDates:['#F{randomH()}']})" /> |
6)有效日期
使用無效日期可以很方便的禁用不可用的日期,但是在只需要啓用少部分日期的情況下,有效日期的功能就非常適合了.
關鍵屬性: opposite 默認爲false, 爲true時,無效日期變成有效日期,該屬性對無效天,特殊天不起作用
示例6.1:只啓用 每個月份的 5日 15日 25日
1 | < input id = "d46" type = "text" class = "Wdate" onFocus = "WdatePicker({opposite:true,disabledDates:['5$']})" /> |
2 | //注意 :'5$' 表示以 5 結尾 注意 $ 的用法 |
7)特殊天和特殊日期
特殊天和特殊日期的用法跟完全無效天和無效日期完全相同,但是opposite屬性對其無效
關鍵屬性:
specialDays (0至6 分別代表 週日至週六) 用法同無效天
specialDates 用法同無效日期,但是對時分秒無效
示例7.1:高亮每週 週一 週五
1 | < input id = "d471" type = "text" class = "Wdate" onFocus = "WdatePicker({specialDays:[1,5]})" /> |
示例7.2:高亮每月 1號 15號
1 | < input id = "d472" type = "text" class = "Wdate" onFocus = "WdatePicker({specialDates:['....-..-01','....-..-15']})" /> |
大功告成O(∩_∩)O~~ Thanks: http://www.my97.net/