w-picker.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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.toString());
  11. let endDate=new Date(end.toString());
  12. if(start>end){
  13. initstartDate=new Date(end.toString());
  14. endDate=new Date(start.toString());
  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=[],dvalDate=[];
  20. switch(mode){
  21. case "half":
  22. dvalDate=flag?[...value.split(" ")[0].split("-"),...value.split(" ")[1].split(":")]:[...value.split(" ")[0].split("-"),value.split(" ")[1]];
  23. break;
  24. case "date":
  25. case "yearMonth":
  26. dvalDate=value.split("-");
  27. break;
  28. case "dateTime":
  29. dvalDate=[...value.split(" ")[0].split("-"),...value.split(" ")[1].split(":")];
  30. break;
  31. case "time":
  32. dvalDate=value.split(":");
  33. break;
  34. }
  35. let curMonth=flag?dvalDate[1]*1:(dvalDate[1]+1);
  36. let dYear=aToday.getFullYear();
  37. let dMonth=aToday.getMonth()+1;
  38. let dDate=aToday.getDate();
  39. let totalDays=new Date(startYear,curMonth,0).getDate();
  40. let dvalObj={};
  41. switch(mode){
  42. case "half":
  43. case "date":
  44. case "yearMonth":
  45. let curYear=dvalDate[0];
  46. let curMonth=dvalDate[1];
  47. if(disabled){
  48. for(let s=startYear;s<=dYear;s++){
  49. years.push(s+'');
  50. };
  51. if(curYear==dYear){
  52. for(let m=1;m<=dMonth;m++){
  53. months.push(forMatNum(m));
  54. };
  55. }else{
  56. for(let m=1;m<=12;m++){
  57. months.push(forMatNum(m));
  58. };
  59. }
  60. if(curMonth==dMonth){
  61. for(let d=1;d<=dDate;d++){
  62. days.push(forMatNum(d));
  63. }
  64. }else{
  65. for(let d=1;d<=totalDays;d++){
  66. days.push(forMatNum(d));
  67. }
  68. }
  69. }else{
  70. for(let s=startYear;s<=endYear;s++){
  71. years.push(s+'');
  72. };
  73. for(let m=1;m<=12;m++){
  74. months.push(forMatNum(m));
  75. };
  76. for(let d=1;d<=totalDays;d++){
  77. days.push(forMatNum(d));
  78. }
  79. };
  80. break;
  81. default:
  82. for(let s=startYear;s<=endYear;s++){
  83. years.push(s+'');
  84. };
  85. for(let m=1;m<=12;m++){
  86. months.push(forMatNum(m));
  87. };
  88. for(let d=1;d<=totalDays;d++){
  89. days.push(forMatNum(d));
  90. }
  91. break;
  92. }
  93. for(let h=0;h<24;h++){
  94. hours.push(forMatNum(h));
  95. }
  96. for(let m=0;m<60;m+=step*1){
  97. minutes.push(forMatNum(m));
  98. }
  99. for(let s=0;s<60;s++){
  100. seconds.push(forMatNum(s));
  101. }
  102. if(flag){
  103. returnArr=[
  104. years.indexOf(dvalDate[0]),
  105. months.indexOf(dvalDate[1]),
  106. days.indexOf(dvalDate[2]),
  107. hours.indexOf(dvalDate[3]),
  108. minutes.indexOf(dvalDate[4])==-1?0:minutes.indexOf(dvalDate[4]),
  109. seconds.indexOf(dvalDate[5])
  110. ]
  111. }
  112. switch(mode){
  113. case "date":
  114. if(flag){
  115. defaultVal=[returnArr[0],returnArr[1],returnArr[2]];
  116. return {years,months,days,defaultVal}
  117. }else{
  118. defaultVal=[
  119. years.indexOf(dvalDate[0])==-1?0:years.indexOf(dvalDate[0]),
  120. months.indexOf(dvalDate[1])==-1?0:months.indexOf(dvalDate[1]),
  121. days.indexOf(dvalDate[2])==-1?0:days.indexOf(dvalDate[2])
  122. ];
  123. return {years,months,days,defaultVal}
  124. }
  125. break;
  126. case "half":
  127. areas=[{
  128. label:"上午",
  129. value:0
  130. },{
  131. label:"下午",
  132. value:1
  133. }];
  134. if(flag){
  135. defaultVal=[returnArr[0],returnArr[1],returnArr[2],returnArr[3]];
  136. return {years,months,days,areas,defaultVal}
  137. }else{
  138. let idx=0;
  139. areas.map((v,k)=>{
  140. if(v.label==dvalDate[3]){
  141. idx=v.value;
  142. }
  143. })
  144. defaultVal=[
  145. years.indexOf(dvalDate[0])==-1?0:years.indexOf(dvalDate[0]),
  146. months.indexOf(dvalDate[1])==-1?0:months.indexOf(dvalDate[1]),
  147. days.indexOf(dvalDate[2])==-1?0:days.indexOf(dvalDate[2]),
  148. idx
  149. ];
  150. return {years,months,days,areas,defaultVal}
  151. }
  152. break;
  153. case "yearMonth":
  154. if(flag){
  155. defaultVal=[returnArr[0],returnArr[1]];
  156. return {years,months,defaultVal}
  157. }else{
  158. defaultVal=[
  159. years.indexOf(dvalDate[0])==-1?0:years.indexOf(dvalDate[0]),
  160. months.indexOf(dvalDate[1])==-1?0:months.indexOf(dvalDate[1])
  161. ];
  162. return {years,months,defaultVal}
  163. }
  164. break;
  165. case "dateTime":
  166. if(flag){
  167. defaultVal=returnArr;
  168. }else{
  169. defaultVal=[
  170. years.indexOf(dvalDate[0])==-1?0:years.indexOf(dvalDate[0]),
  171. months.indexOf(dvalDate[1])==-1?0:months.indexOf(dvalDate[1]),
  172. days.indexOf(dvalDate[2])==-1?0:days.indexOf(dvalDate[2]),
  173. hours.indexOf(dvalDate[3])==-1?0:hours.indexOf(dvalDate[3]),
  174. minutes.indexOf(dvalDate[4])==-1?0:minutes.indexOf(dvalDate[4]),
  175. seconds.indexOf(dvalDate[5])==-1?0:seconds.indexOf(dvalDate[5])
  176. ];
  177. }
  178. return {years,months,days,hours,minutes,seconds,defaultVal}
  179. break;
  180. case "time":
  181. if(flag){
  182. defaultVal=[returnArr[3],returnArr[4],returnArr[5]];
  183. }else{
  184. defaultVal=[
  185. hours.indexOf(dvalDate[0])==-1?0:hours.indexOf(dvalDate[0]),
  186. minutes.indexOf(dvalDate[1])==-1?0:minutes.indexOf(dvalDate[1]),
  187. seconds.indexOf(dvalDate[2])==-1?0:seconds.indexOf(dvalDate[2])
  188. ];
  189. }
  190. return {hours,minutes,seconds,defaultVal}
  191. break;
  192. }
  193. },
  194. initMonths:(year,disabled)=>{
  195. let aDate=new Date();
  196. let dYear=aDate.getFullYear();
  197. let dMonth=aDate.getMonth()+1;
  198. let dDate=aDate.getDate();
  199. let flag=dYear==year?true:false;
  200. let months=[];
  201. if(disabled){
  202. if(flag){
  203. for(let m=1;m<=dMonth;m++){
  204. months.push(forMatNum(m));
  205. };
  206. }else{
  207. for(let m=1;m<=12;m++){
  208. months.push(forMatNum(m));
  209. };
  210. }
  211. }else{
  212. for(let m=1;m<=12;m++){
  213. months.push(forMatNum(m));
  214. };
  215. };
  216. return months;
  217. },
  218. initDays:(year,month,disabled)=>{
  219. let aDate=new Date();
  220. let dYear=aDate.getFullYear();
  221. let dMonth=aDate.getMonth()+1;
  222. let dDate=aDate.getDate();
  223. let flag=(dYear==year&&dMonth==month)?true:false;
  224. let totalDays=new Date(year,month,0).getDate();
  225. let dates=[];
  226. if(flag&&disabled){
  227. for(let d=1;d<=dDate;d++){
  228. dates.push(forMatNum(d));
  229. };
  230. }else{
  231. for(let d=1;d<=totalDays;d++){
  232. dates.push(forMatNum(d));
  233. };
  234. };
  235. return dates;
  236. },
  237. },
  238. //短期日期上下午
  239. limitHour:{
  240. init(dayStep=7,dVal){
  241. let startDate=new Date();
  242. let date=[],areas=[],hours=[];
  243. let hour=new Date().getHours();
  244. let weeks=["周日","周一","周二","周三","周四","周五","周六"];
  245. let arrs=[];
  246. let defaultVal=[];
  247. let d=0,a=0,h=0;
  248. for(let i=0;i<dayStep;i++){
  249. let year,month,day,weekday;
  250. year=startDate.getFullYear();
  251. month=forMatNum(startDate.getMonth()+1);
  252. day=forMatNum(startDate.getDate());
  253. weekday=weeks[startDate.getDay()];
  254. let label="";
  255. switch(i){
  256. case 0:
  257. label="今天";
  258. break;
  259. case 1:
  260. label="明天"
  261. break;
  262. case 2:
  263. label="后天"
  264. break;
  265. default:
  266. label=month+"月"+day+"日"+" "+weekday;
  267. break;
  268. }
  269. date.push({
  270. label:label,
  271. value:year+"-"+month+"-"+day,
  272. today:i==0?true:false
  273. })
  274. startDate.setDate(startDate.getDate()+1);
  275. }
  276. if(hour>12){
  277. areas=[{
  278. label:"下午",
  279. value:1
  280. }]
  281. }else{
  282. areas=[{
  283. label:"上午",
  284. value:0
  285. },{
  286. label:"下午",
  287. value:1
  288. }]
  289. };
  290. for(let k=hour>12?hour-12:hour;k<=12;k++){
  291. hours.push({
  292. label:forMatNum(k),
  293. value:forMatNum(hour>12?k+12:k)
  294. })
  295. };
  296. date.map((v,k)=>{
  297. if(v.label==dVal[0]){
  298. d=k
  299. }
  300. })
  301. if(d!=0){
  302. areas=this.initAreas(date[d]);
  303. hours=this.initHours(date[d],areas[a]);
  304. }
  305. areas.map((v,k)=>{
  306. if(v.label==dVal[1]){
  307. a=k
  308. }
  309. })
  310. hours.map((v,k)=>{
  311. if(v.label==dVal[2]){
  312. h=k
  313. }
  314. });
  315. defaultVal=[d,a,h]
  316. return {date,areas,hours,defaultVal};
  317. },
  318. initAreas(date){
  319. let areas=[];
  320. let hour=new Date().getHours();
  321. if(date.today){
  322. if(hour>12){
  323. areas=[{
  324. label:"下午",
  325. value:1
  326. }]
  327. }else{
  328. areas=[{
  329. label:"上午",
  330. value:0
  331. },{
  332. label:"下午",
  333. value:1
  334. }]
  335. };
  336. }else{
  337. areas=[{
  338. label:"上午",
  339. value:0
  340. },{
  341. label:"下午",
  342. value:1
  343. }]
  344. }
  345. return areas;
  346. },
  347. initHours(dateCol,hourCol){
  348. let hours=[];
  349. let hour=new Date().getHours();
  350. if(dateCol.today){
  351. if(hourCol.value==1&&hour<=12){
  352. for(let k=1;k<=12;k++){
  353. hours.push({
  354. label:forMatNum(k),
  355. value:forMatNum(hourCol.value==1?k+12:k)
  356. })
  357. };
  358. }else{
  359. for(let k=hour>12?hour-12:hour;k<=12;k++){
  360. hours.push({
  361. label:forMatNum(k),
  362. value:forMatNum(hourCol.value==1?k+12:k)
  363. })
  364. };
  365. }
  366. }else{
  367. for(let k=1;k<=12;k++){
  368. hours.push({
  369. label:forMatNum(k),
  370. value:forMatNum(hourCol.value==1?k+12:k)
  371. })
  372. };
  373. };
  374. return hours
  375. }
  376. },
  377. //短期日期时间初始化
  378. limit:{
  379. init(dayStep=7,startHour=8,endHour=20,minuteStep=1,afterStep=30,dVal){
  380. let startDate=new Date();
  381. let bsDate=new Date(new Date().getTime()+afterStep*60*1000);
  382. let date=[],hours=[],minutes=[];
  383. let hour=bsDate.getHours();
  384. let minute=Math.floor(bsDate.getMinutes()/minuteStep)*minuteStep;
  385. let weeks=["周日","周一","周二","周三","周四","周五","周六"];
  386. let d=0,h=0,m=0;
  387. let defaultVal=[];
  388. for(let i=0;i<dayStep;i++){
  389. let year,month,day,weekday;
  390. year=startDate.getFullYear();
  391. month=forMatNum(startDate.getMonth()+1);
  392. day=forMatNum(startDate.getDate());
  393. weekday=weeks[startDate.getDay()];
  394. let label="";
  395. switch(i){
  396. case 0:
  397. label="今天";
  398. break;
  399. case 1:
  400. label="明天"
  401. break;
  402. case 2:
  403. label="后天"
  404. break;
  405. default:
  406. label=month+"月"+day+"日"+" "+weekday;
  407. break;
  408. }
  409. date.push({
  410. label:label,
  411. value:year+"-"+month+"-"+day,
  412. flag:i==0?true:false
  413. })
  414. startDate.setDate(startDate.getDate()+1);
  415. }
  416. if(hour<startHour){
  417. hour=startHour;
  418. };
  419. if(hour>endHour){
  420. hour=endHour;
  421. };
  422. for(let k=hour*1;k<=endHour*1;k++){
  423. hours.push({
  424. label:forMatNum(k),
  425. value:forMatNum(k),
  426. flag:k==hour?true:false
  427. })
  428. };
  429. for(let j=minute;j<60;j+=minuteStep*1){
  430. minutes.push({
  431. label:forMatNum(j),
  432. value:forMatNum(j)
  433. });
  434. }
  435. date.map((v,k)=>{
  436. if(v.label==dVal[0]){
  437. d=k
  438. }
  439. })
  440. if(d!=0){
  441. hours=this.initHours(startHour=8,endHour=20,minuteStep=1,afterStep=30,date[d].value);
  442. }
  443. hours.map((v,k)=>{
  444. if(v.label==dVal[1]){
  445. h=k
  446. }
  447. })
  448. minutes.map((v,k)=>{
  449. if(v.label==dVal[2]){
  450. m=k
  451. }
  452. })
  453. defaultVal=[d,h,m];
  454. return {date,hours,minutes,defaultVal};
  455. },
  456. initHours(startHour=8,endHour=20,minuteStep=1,afterStep=30,date){
  457. let hours=[];
  458. let arr=date.split("-");
  459. let aDate=new Date();
  460. let dYear=aDate.getFullYear();
  461. let dMonth=aDate.getMonth()+1;
  462. let dDate=aDate.getDate();
  463. let bsDate=new Date(new Date().getTime()+afterStep*60*1000);
  464. let hour=bsDate.getHours();
  465. let flag=(dYear==arr[0]&&dMonth==arr[1]&&dDate==arr[2])?true:false;
  466. if(hour>endHour){
  467. hour=endHour;
  468. };
  469. if(flag){
  470. for(let k=hour*1;k<=endHour*1;k++){
  471. hours.push({
  472. label:forMatNum(k),
  473. value:forMatNum(k),
  474. flag:k==hour?true:false
  475. })
  476. };
  477. }else{
  478. for(let k=startHour*1;k<=endHour*1;k++){
  479. hours.push({
  480. label:forMatNum(k),
  481. value:forMatNum(k),
  482. flag:false
  483. })
  484. }
  485. };
  486. return hours;
  487. },
  488. initMinutes(startHour=8,endHour=20,minuteStep=1,afterStep=30,date,hour){
  489. let minutes=[];
  490. let bsDate=new Date(new Date().getTime()+afterStep*60*1000);
  491. let arr=date.split("-");
  492. let aDate=new Date();
  493. let dYear=aDate.getFullYear();
  494. let dMonth=aDate.getMonth()+1;
  495. let dDate=aDate.getDate();
  496. let dHour=bsDate.getHours();;
  497. let minute=Math.floor(bsDate.getMinutes()/minuteStep)*minuteStep;
  498. let flag=(dYear==arr[0]&&dMonth==arr[1]&&dDate==arr[2])?true:false;
  499. if(flag){
  500. if(hour==dHour){
  501. for(let j=minute;j<60;j+=minuteStep*1){
  502. minutes.push({
  503. label:forMatNum(j),
  504. value:forMatNum(j)
  505. });
  506. }
  507. }else{
  508. for(let j=0;j<60;j+=minuteStep*1){
  509. minutes.push({
  510. label:forMatNum(j),
  511. value:forMatNum(j)
  512. })
  513. }
  514. }
  515. }else{
  516. for(let j=0;j<60;j+=minuteStep*1){
  517. minutes.push({
  518. label:forMatNum(j),
  519. value:forMatNum(j)
  520. })
  521. }
  522. }
  523. return minutes;
  524. }
  525. },
  526. //选择区间初始化
  527. range:{
  528. init(start,end,value,flag){
  529. let aToday=new Date();
  530. let tYear,tMonth,tDay,tHours,tMinutes,tSeconds,defaultVal=[];
  531. let initstartDate=new Date(start.toString());
  532. let endDate=new Date(end.toString());
  533. if(start>end){
  534. initstartDate=new Date(end.toString());
  535. endDate=new Date(start.toString());
  536. };
  537. let startYear=initstartDate.getFullYear();
  538. let startMonth=initstartDate.getMonth()+1;
  539. let endYear=endDate.getFullYear();
  540. let fyears=[],fmonths=[],fdays=[],tyears=[],tmonths=[],tdays=[],returnArr=[],startDVal=[],endDVal=[];
  541. startDVal=value[0].split("-");
  542. endDVal=value[1].split("-");
  543. let curMonth=flag?startDVal[1]*1:(startDVal[1]+1);
  544. let totalDays=new Date(startYear,curMonth,0).getDate();
  545. for(let s=startYear;s<=endYear;s++){
  546. fyears.push(s+'');
  547. };
  548. for(let m=1;m<=12;m++){
  549. fmonths.push(forMatNum(m));
  550. };
  551. for(let d=1;d<=totalDays;d++){
  552. fdays.push(forMatNum(d));
  553. };
  554. for(let s=startDVal[0];s<=endYear;s++){
  555. tyears.push(s+'');
  556. };
  557. for(let m=startDVal[1];m<=12;m++){
  558. tmonths.push(forMatNum(m));
  559. };
  560. for(let d=startDVal[2];d<=totalDays;d++){
  561. tdays.push(forMatNum(d));
  562. };
  563. defaultVal=[
  564. fyears.indexOf(startDVal[0])==-1?0:fyears.indexOf(startDVal[0]),
  565. fmonths.indexOf(startDVal[1])==-1?0:fmonths.indexOf(startDVal[1]),
  566. fdays.indexOf(startDVal[2])==-1?0:fdays.indexOf(startDVal[2]),
  567. 0,
  568. tyears.indexOf(endDVal[0])==-1?0:tyears.indexOf(endDVal[0]),
  569. tmonths.indexOf(endDVal[1])==-1?0:tmonths.indexOf(endDVal[1]),
  570. tdays.indexOf(endDVal[2])==-1?0:tdays.indexOf(endDVal[2])
  571. ];
  572. return {
  573. fyears,
  574. fmonths,
  575. fdays,
  576. tyears,
  577. tmonths,
  578. tdays,
  579. defaultVal
  580. }
  581. },
  582. initStartDays(year,month){
  583. let totalDays=new Date(year,month,0).getDate();
  584. let dates=[];
  585. for(let d=1;d<=totalDays;d++){
  586. dates.push(forMatNum(d));
  587. };
  588. return dates;
  589. },
  590. initEndYears(curYear,startYear,endYear){
  591. let years=[];
  592. for(let y=curYear;y<=endYear;y++){
  593. years.push(forMatNum(y));
  594. };
  595. return years;
  596. },
  597. initEndMonths(curMonth){
  598. let months=[];
  599. for(let m=curMonth*1;m<=12;m++){
  600. months.push(forMatNum(m));
  601. };
  602. return months;
  603. },
  604. initEndDays(curYear,curMonth,curDate,tYear,tMonth){
  605. let totalDays=new Date(curYear,curMonth,0).getDate();
  606. let days=[];
  607. for(let d=curDate*1;d<=totalDays;d++){
  608. days.push(forMatNum(d));
  609. };
  610. return days;
  611. },
  612. initToMonths(curYear,curMonth,curDate,tYear){
  613. let aDate=new Date(curYear,curMonth,curDate).getTime();
  614. let bDate=new Date(tYear,curMonth,curDate).getTime();
  615. let months=[];
  616. if(bDate-aDate>0){
  617. console.log(1)
  618. for(let m=1;m<=12;m++){
  619. months.push(forMatNum(m));
  620. };
  621. }else{
  622. for(let m=curMonth*1;m<=12;m++){
  623. months.push(forMatNum(m));
  624. };
  625. }
  626. return months;
  627. },
  628. initToDays(curYear,curMonth,curDate,tYear,tMonth){
  629. let aDate=new Date(curYear,curMonth,curDate).getTime();
  630. let bDate=new Date(tYear,tMonth,curDate).getTime();
  631. let totalDays=new Date(tYear,tMonth,0).getDate();
  632. let days=[];
  633. if(bDate-aDate>0){
  634. for(let d=1;d<=totalDays;d++){
  635. days.push(forMatNum(d));
  636. };
  637. }else{
  638. for(let d=curDate*1;d<=totalDays;d++){
  639. days.push(forMatNum(d));
  640. };
  641. }
  642. return days;
  643. }
  644. }
  645. }
  646. export default initPicker