w-picker.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. const forMatNum=(num)=>{
  2. return num<10?'0'+num:num+'';
  3. }
  4. const initPicker={
  5. //日期
  6. date:{
  7. init(start,end,mode="date",step,value,flag,disabled){
  8. let aToday=new Date();
  9. let tYear,tMonth,tDay,tHours,tMinutes,tSeconds,defaultVal=[];
  10. let initstartDate=new Date(start);
  11. let endDate=new Date(end);
  12. if(start>end){
  13. initstartDate=new Date(end);
  14. endDate=new Date(start);
  15. };
  16. let startYear=initstartDate.getFullYear();
  17. let startMonth=initstartDate.getMonth()+1;
  18. let endYear=endDate.getFullYear();
  19. let years=[],months=[],days=[],hours=[],minutes=[],seconds=[],areas=[],returnArr=[];
  20. let curMonth=flag?value[1]*1:(value[1]+1);
  21. let dYear=aToday.getFullYear();
  22. let dMonth=aToday.getMonth()+1;
  23. let dDate=aToday.getDate();
  24. let totalDays=new Date(startYear,curMonth,0).getDate();
  25. for(let s=startYear;s<=endYear;s++){
  26. years.push(s+'');
  27. };
  28. let curYear=years[value[0]];
  29. switch(mode){
  30. case "half":
  31. case "date":
  32. case "yearMonth":
  33. if(disabled&&curYear==dYear){
  34. for(let m=1;m<=dMonth;m++){
  35. months.push(forMatNum(m));
  36. };
  37. for(let d=1;d<=dDate;d++){
  38. days.push(forMatNum(d));
  39. }
  40. }else{
  41. for(let m=1;m<=12;m++){
  42. months.push(forMatNum(m));
  43. };
  44. for(let d=1;d<=totalDays;d++){
  45. days.push(forMatNum(d));
  46. }
  47. };
  48. break;
  49. default:
  50. for(let m=1;m<=12;m++){
  51. months.push(forMatNum(m));
  52. };
  53. for(let d=1;d<=totalDays;d++){
  54. days.push(forMatNum(d));
  55. }
  56. break;
  57. }
  58. for(let h=0;h<24;h++){
  59. hours.push(forMatNum(h));
  60. }
  61. for(let m=0;m<60;m+=step*1){
  62. minutes.push(forMatNum(m));
  63. }
  64. for(let s=0;s<60;s++){
  65. seconds.push(forMatNum(s));
  66. }
  67. if(flag){
  68. returnArr=[
  69. years.indexOf(value[0]),
  70. months.indexOf(value[1]),
  71. days.indexOf(value[2]),
  72. hours.indexOf(value[3]),
  73. minutes.indexOf(value[4])==-1?0:minutes.indexOf(value[4]),
  74. seconds.indexOf(value[5])
  75. ]
  76. };
  77. switch(mode){
  78. case "range":
  79. if(flag){
  80. defaultVal=[returnArr[0],returnArr[1],returnArr[2],0,returnArr[0],returnArr[1],returnArr[2]];
  81. return {years,months,days,defaultVal}
  82. }else{
  83. return {years,months,days}
  84. }
  85. break;
  86. case "date":
  87. if(flag){
  88. defaultVal=[returnArr[0],returnArr[1],returnArr[2]];
  89. return {years,months,days,defaultVal}
  90. }else{
  91. return {years,months,days}
  92. }
  93. break;
  94. case "half":
  95. areas=[{
  96. label:"上午",
  97. value:0
  98. },{
  99. label:"下午",
  100. value:1
  101. }];
  102. if(flag){
  103. defaultVal=[returnArr[0],returnArr[1],returnArr[2],returnArr[3]];
  104. return {years,months,days,areas,defaultVal}
  105. }else{
  106. return {years,months,days,areas}
  107. }
  108. break;
  109. case "yearMonth":
  110. if(flag){
  111. defaultVal=[returnArr[0],returnArr[1]];
  112. return {years,months,defaultVal}
  113. }else{
  114. return {years,months}
  115. }
  116. break;
  117. case "dateTime":
  118. if(flag){
  119. defaultVal=returnArr;
  120. return {years,months,days,hours,minutes,seconds,defaultVal}
  121. }else{
  122. return {years,months,days,hours,minutes,seconds}
  123. }
  124. break;
  125. case "time":
  126. if(flag){
  127. defaultVal=[returnArr[3],returnArr[4],returnArr[5]];
  128. return {hours,minutes,seconds,defaultVal}
  129. }else{
  130. return {hours,minutes,seconds}
  131. }
  132. break;
  133. }
  134. },
  135. initMonths:(year,disabled)=>{
  136. let aDate=new Date();
  137. let dYear=aDate.getFullYear();
  138. let dMonth=aDate.getMonth()+1;
  139. let dDate=aDate.getDate();
  140. let flag=dYear==year?true:false;
  141. let months=[];
  142. if(flag&&disabled){
  143. for(let m=1;m<=dMonth;m++){
  144. months.push(forMatNum(m));
  145. };
  146. }else{
  147. for(let m=1;m<=12;m++){
  148. months.push(forMatNum(m));
  149. };
  150. };
  151. return months;
  152. },
  153. initDays:(year,month,disabled)=>{
  154. let aDate=new Date();
  155. let dYear=aDate.getFullYear();
  156. let dMonth=aDate.getMonth()+1;
  157. let dDate=aDate.getDate();
  158. let flag=(dYear==year&&dMonth==month)?true:false;
  159. let totalDays=new Date(year,month,0).getDate();
  160. let dates=[];
  161. if(flag&&disabled){
  162. for(let d=1;d<=dDate;d++){
  163. dates.push(forMatNum(d));
  164. };
  165. }else{
  166. for(let d=1;d<=totalDays;d++){
  167. dates.push(forMatNum(d));
  168. };
  169. };
  170. return dates;
  171. },
  172. },
  173. //短期日期上下午
  174. limitHour:{
  175. init(dayStep=7){
  176. let startDate=new Date();
  177. let date=[],areas=[],hours=[];
  178. let hour=new Date().getHours();
  179. let weeks=["周日","周一","周二","周三","周四","周五","周六"];
  180. let arrs=[];
  181. for(let i=0;i<dayStep;i++){
  182. let year,month,day,weekday;
  183. year=startDate.getFullYear();
  184. month=forMatNum(startDate.getMonth()+1);
  185. day=forMatNum(startDate.getDate());
  186. weekday=weeks[startDate.getDay()];
  187. let label="";
  188. switch(i){
  189. case 0:
  190. label="今天";
  191. break;
  192. case 1:
  193. label="明天"
  194. break;
  195. case 2:
  196. label="后天"
  197. break;
  198. default:
  199. label=month+"月"+day+"日"+" "+weekday;
  200. break;
  201. }
  202. date.push({
  203. label:label,
  204. value:year+"-"+month+"-"+day,
  205. today:i==0?true:false
  206. })
  207. startDate.setDate(startDate.getDate()+1);
  208. }
  209. if(hour>12){
  210. areas=[{
  211. label:"下午",
  212. value:1
  213. }]
  214. }else{
  215. areas=[{
  216. label:"上午",
  217. value:0
  218. },{
  219. label:"下午",
  220. value:1
  221. }]
  222. };
  223. for(let k=hour>12?hour-12:hour;k<=12;k++){
  224. hours.push({
  225. label:forMatNum(k),
  226. value:forMatNum(hour>12?k+12:k)
  227. })
  228. };
  229. return {date,areas,hours};
  230. },
  231. initAreas(date){
  232. let areas=[];
  233. let hour=new Date().getHours();
  234. if(date.today){
  235. if(hour>12){
  236. areas=[{
  237. label:"下午",
  238. value:1
  239. }]
  240. }else{
  241. areas=[{
  242. label:"上午",
  243. value:0
  244. },{
  245. label:"下午",
  246. value:1
  247. }]
  248. };
  249. }else{
  250. areas=[{
  251. label:"上午",
  252. value:0
  253. },{
  254. label:"下午",
  255. value:1
  256. }]
  257. }
  258. return areas;areas=[{
  259. label:"上午",
  260. value:0
  261. },{
  262. label:"下午",
  263. value:1
  264. }]
  265. },
  266. initHours(dateCol,hourCol){
  267. let hours=[];
  268. let hour=new Date().getHours();
  269. if(dateCol.today){
  270. if(hourCol.value==1&&hour<=12){
  271. for(let k=1;k<=12;k++){
  272. hours.push({
  273. label:forMatNum(k),
  274. value:forMatNum(hourCol.value==1?k+12:k)
  275. })
  276. };
  277. }else{
  278. for(let k=hour>12?hour-12:hour;k<=12;k++){
  279. hours.push({
  280. label:forMatNum(k),
  281. value:forMatNum(hourCol.value==1?k+12:k)
  282. })
  283. };
  284. }
  285. }else{
  286. for(let k=1;k<=12;k++){
  287. hours.push({
  288. label:forMatNum(k),
  289. value:forMatNum(hourCol.value==1?k+12:k)
  290. })
  291. };
  292. };
  293. return hours
  294. }
  295. },
  296. //短期日期时间初始化
  297. limit:{
  298. init(dayStep=7,startHour=8,endHour=20,minuteStep=1,afterStep=30){
  299. let startDate=new Date();
  300. let bsDate=new Date(new Date().getTime()+afterStep*60*1000);
  301. let date=[],hours=[],minutes=[];
  302. let hour=bsDate.getHours();
  303. let minute=Math.floor(bsDate.getMinutes()/minuteStep)*minuteStep;
  304. let weeks=["周日","周一","周二","周三","周四","周五","周六"];
  305. for(let i=0;i<dayStep;i++){
  306. let year,month,day,weekday;
  307. year=startDate.getFullYear();
  308. month=forMatNum(startDate.getMonth()+1);
  309. day=forMatNum(startDate.getDate());
  310. weekday=weeks[startDate.getDay()];
  311. let label="";
  312. switch(i){
  313. case 0:
  314. label="今天";
  315. break;
  316. case 1:
  317. label="明天"
  318. break;
  319. case 2:
  320. label="后天"
  321. break;
  322. default:
  323. label=month+"月"+day+"日"+" "+weekday;
  324. break;
  325. }
  326. date.push({
  327. label:label,
  328. value:year+"-"+month+"-"+day,
  329. flag:i==0?true:false
  330. })
  331. startDate.setDate(startDate.getDate()+1);
  332. }
  333. if(hour<startHour){
  334. hour=startHour;
  335. };
  336. if(hour>endHour){
  337. hour=endHour;
  338. };
  339. for(let k=hour*1;k<=endHour*1;k++){
  340. hours.push({
  341. label:forMatNum(k),
  342. value:forMatNum(k),
  343. flag:k==hour?true:false
  344. })
  345. };
  346. for(let j=minute;j<60;j+=minuteStep*1){
  347. minutes.push({
  348. label:forMatNum(j),
  349. value:forMatNum(j)
  350. });
  351. }
  352. return {date,hours,minutes};
  353. },
  354. initHours(startHour=8,endHour=20,minuteStep=1,afterStep=30,date){
  355. let hours=[];
  356. let arr=date.split("-");
  357. let aDate=new Date();
  358. let dYear=aDate.getFullYear();
  359. let dMonth=aDate.getMonth()+1;
  360. let dDate=aDate.getDate();
  361. let bsDate=new Date(new Date().getTime()+afterStep*60*1000);
  362. let hour=bsDate.getHours();
  363. let flag=(dYear==arr[0]&&dMonth==arr[1]&&dDate==arr[2])?true:false;
  364. if(hour>endHour){
  365. hour=endHour;
  366. };
  367. if(flag){
  368. for(let k=hour*1;k<=endHour*1;k++){
  369. hours.push({
  370. label:forMatNum(k),
  371. value:forMatNum(k),
  372. flag:k==hour?true:false
  373. })
  374. };
  375. }else{
  376. for(let k=startHour*1;k<=endHour*1;k++){
  377. hours.push({
  378. label:forMatNum(k),
  379. value:forMatNum(k),
  380. flag:false
  381. })
  382. }
  383. };
  384. return hours;
  385. },
  386. initMinutes(startHour=8,endHour=20,minuteStep=1,afterStep=30,date,hour){
  387. let minutes=[];
  388. let bsDate=new Date(new Date().getTime()+afterStep*60*1000);
  389. let arr=date.split("-");
  390. let aDate=new Date();
  391. let dYear=aDate.getFullYear();
  392. let dMonth=aDate.getMonth()+1;
  393. let dDate=aDate.getDate();
  394. let dHour=bsDate.getHours();;
  395. let minute=Math.floor(bsDate.getMinutes()/minuteStep)*minuteStep;
  396. let flag=(dYear==arr[0]&&dMonth==arr[1]&&dDate==arr[2])?true:false;
  397. if(flag){
  398. if(hour==dHour){
  399. for(let j=minute;j<60;j+=minuteStep*1){
  400. minutes.push({
  401. label:forMatNum(j),
  402. value:forMatNum(j)
  403. });
  404. }
  405. }else{
  406. for(let j=0;j<60;j+=minuteStep*1){
  407. minutes.push({
  408. label:forMatNum(j),
  409. value:forMatNum(j)
  410. })
  411. }
  412. }
  413. }else{
  414. for(let j=0;j<60;j+=minuteStep*1){
  415. minutes.push({
  416. label:forMatNum(j),
  417. value:forMatNum(j)
  418. })
  419. }
  420. }
  421. return minutes;
  422. }
  423. },
  424. //选择区间初始化
  425. range:{
  426. init(start,end,value,flag){
  427. let aToday=new Date();
  428. let tYear,tMonth,tDay,tHours,tMinutes,tSeconds,defaultVal=[];
  429. let initstartDate=new Date(start);
  430. let endDate=new Date(end);
  431. if(start>end){
  432. initstartDate=new Date(end);
  433. endDate=new Date(start);
  434. };
  435. let startYear=initstartDate.getFullYear();
  436. let startMonth=initstartDate.getMonth()+1;
  437. let endYear=endDate.getFullYear();
  438. let fyears=[],fmonths=[],fdays=[],tyears=[],tmonths=[],tdays=[],returnArr=[];
  439. let curMonth=flag?value[1]*1:(value[1]+1);
  440. let totalDays=new Date(startYear,curMonth,0).getDate();
  441. for(let s=startYear;s<=endYear;s++){
  442. fyears.push(s+'');
  443. };
  444. for(let m=1;m<=12;m++){
  445. fmonths.push(forMatNum(m));
  446. };
  447. for(let d=1;d<=totalDays;d++){
  448. fdays.push(forMatNum(d));
  449. };
  450. for(let s=startYear;s<=endYear;s++){
  451. tyears.push(s+'');
  452. };
  453. for(let m=1;m<=12;m++){
  454. tmonths.push(forMatNum(m));
  455. };
  456. for(let d=1;d<=totalDays;d++){
  457. tdays.push(forMatNum(d));
  458. };
  459. if(flag){
  460. defaultVal=[
  461. fyears.indexOf(value[0]),
  462. fmonths.indexOf(value[1]),
  463. fdays.indexOf(value[2]),
  464. 0,
  465. tyears.indexOf(value[0]),
  466. tmonths.indexOf(value[1]),
  467. tdays.indexOf(value[2])
  468. ];
  469. return {
  470. fyears,
  471. fmonths,
  472. fdays,
  473. tyears,
  474. tmonths,
  475. tdays,
  476. defaultVal
  477. }
  478. }else{
  479. return {
  480. fyears,
  481. fmonths,
  482. fdays,
  483. tyears,
  484. tmonths,
  485. tdays,
  486. }
  487. }
  488. },
  489. initDays(year,month){
  490. let totalDays=new Date(year,month,0).getDate();
  491. let dates=[];
  492. for(let d=1;d<=totalDays;d++){
  493. dates.push(forMatNum(d));
  494. };
  495. return dates;
  496. }
  497. }
  498. }
  499. export default initPicker