jedate.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /**
  2. @Name : jeDate v2.0 日期控件
  3. @Author: chne guojun
  4. @Date: 2015-12-28
  5. @QQ群:516754269
  6. @Site:https://github.com/singod/jeDate
  7. */
  8. (function(win) {
  9. var jeDt = {}, doc = document, Cell = "#jedatebox";
  10. /* (tag), (#id), (.className) ,(tag > .className) ,(tag > tag) ,(#id > tag.className) ,
  11. (.className tag) ,(tag, tag, #id) ,(tag#id.className) ,(span > * > b) ,(input[name=radio])
  12. */
  13. var QD = jeDt.query =function(){function r(c,g){g=g||document;if(!/^[\w\-_#]+$/.test(c)&&g.querySelectorAll)return m(g.querySelectorAll(c));if(-1<c.indexOf(",")){for(var d=c.split(/,/g),a=[],b=0,e=d.length;b<e;++b)a=a.concat(r(d[b],g));return y(a)}var d=c.match(z),a=d.pop(),e=(a.match(t)||k)[1],f=!e&&(a.match(u)||k)[1],b=!e&&(a.match(v)||k)[1],a=c.match(/\[(?:[\w\-_][^=]+)=(?:[\'\[\]\w\-_]+)\]/g);if(f&&!a&&!b&&g.getElementsByClassName)b=m(g.getElementsByClassName(f));else{b=!e&&m(g.getElementsByTagName(b||"*"));f&&(b=w(b,"className",RegExp("(^|\\s)"+f+"(\\s|$)")));if(e)return(d=g.getElementById(e))?[d]:[];if(a)for(e=0;e<a.length;e++)var f=(a[e].match(x)||k)[1],h=(a[e].match(x)||k)[2],h=h.replace(/\'/g,"").replace(/\-/g,"\\-").replace(/\[/g,"\\[").replace(/\]/g,"\\]"),b=w(b,f,RegExp("(^"+h+"$)"))}return d[0]&&b[0]?p(d,b):b}function m(c){try{return Array.prototype.slice.call(c)}catch(g){for(var d=[],a=0,b=c.length;a<b;++a)d[a]=c[a];return d}}function p(c,g,d){var a=c.pop();if("\x3e"===a)return p(c,g,!0);for(var b=[],e=-1,f=(a.match(t)||k)[1],h=!f&&(a.match(u)||k)[1],a=!f&&(a.match(v)||k)[1],m=-1,q,l,n,a=a&&a.toLowerCase();q=g[++m];){l=q.parentNode;do if(n=(n=(n=!a||"*"===a||a===l.nodeName.toLowerCase())&&(!f||l.id===f))&&(!h||RegExp("(^|\\s)"+h+"(\\s|$)").test(l.className)),d||n)break;while(l=l.parentNode);n&&(b[++e]=q)}return c[0]&&b[0]?p(c,b):b}function w(c,g,d){for(var a=-1,b,e=-1,f=[];b=c[++a];)d.test(b.getAttribute(g))&&(f[++e]=b);return f}var z=/(?:[\*\w\-\\.#]+)+(?:\[(?:[\w\-_][^=]+)=(?:[\'\[\]\w\-_]+)\])*|\*|>/gi,u=/^(?:[\w\-_]+)?\.([\w\-_]+)/,t=/^(?:[\w\-_]+)?#([\w\-_]+)/,v=/^([\w\*\-_]+)/,k=[null,null,null],x=/\[([\w\-_][^=]+)=([\'\[\]\w\-_]+)\]/,y=function(){var c=+new Date,g=function(){var d=1;return function(a){var b=a[c],e=d++;return b?!1:(a[c]=e,!0)}}();return function(d){for(var a=d.length,b=[],e=-1,f=0,h;f<a;++f)h=d[f],g(h)&&(b[++e]=h);c+=1;return b}}();return r}();
  14. jeDt.each = function(arr, fn) {
  15. var i = 0, len = arr.length;
  16. for (;i < len; i++) {
  17. if (fn(i, arr[i]) === false) {
  18. break;
  19. }
  20. }
  21. };
  22. jeDt.extend = function() {
  23. var _extend = function me(dest, source) {
  24. for (var name in dest) {
  25. if (dest.hasOwnProperty(name)) {
  26. //当前属性是否为对象,如果为对象,则进行递归
  27. if (dest[name] instanceof Object && source[name] instanceof Object) {
  28. me(dest[name], source[name]);
  29. }
  30. //检测该属性是否存在
  31. if (source.hasOwnProperty(name)) {
  32. continue;
  33. } else {
  34. source[name] = dest[name];
  35. }
  36. }
  37. }
  38. };
  39. var _result = {}, arr = arguments;
  40. //遍历属性,至后向前
  41. if (!arr.length) return {};
  42. for (var i = arr.length - 1; i >= 0; i--) {
  43. _extend(arr[i], _result);
  44. }
  45. arr[0] = _result;
  46. return _result;
  47. };
  48. jeDt.trim = function(str) {
  49. str = str || "";
  50. return str.replace(/^\s|\s$/g, "").replace(/\s+/g, " ");
  51. };
  52. jeDt.attr = function(elem, key, val) {
  53. if (typeof key === "string" && typeof val === 'undefined') {
  54. return elem.getAttribute(key);
  55. } else {
  56. elem.setAttribute(key, val);
  57. }
  58. return this;
  59. };
  60. jeDt.stopmp = function(e) {
  61. e = e || win.event;
  62. e.stopPropagation ? e.stopPropagation() :e.cancelBubble = true;
  63. return this;
  64. };
  65. jeDt.getCss = function(obj, name) {
  66. if (obj.currentStyle) {
  67. return obj.currentStyle[name];
  68. } else if (window.getComputedStyle) {
  69. return document.defaultView.getComputedStyle(obj)[name];
  70. }
  71. return null;
  72. };
  73. //查询样式是否存在
  74. jeDt.hasClass = function(elem, cls) {
  75. elem = elem || {};
  76. return new RegExp("\\b" + cls + "\\b").test(elem.className);
  77. };
  78. //添加样式
  79. jeDt.addClass = function(elem, cls) {
  80. elem = elem || {};
  81. jeDt.hasClass(elem, cls) || (elem.className += " " + cls);
  82. elem.className = jeDt.trim(elem.className);
  83. return this;
  84. };
  85. //删除样式
  86. jeDt.removeClass = function(elem, cls) {
  87. elem = elem || {};
  88. if (jeDt.hasClass(elem, cls)) {
  89. elem.className = elem.className.replace(new RegExp("(\\s|^)" + cls + "(\\s|$)"), "");
  90. }
  91. return this;
  92. };
  93. //事件监听器
  94. jeDt.on = function(obj, type, fn) {
  95. if (obj.addEventListener) {
  96. obj.addEventListener(type, fn, false);
  97. } else if (obj.attachEvent) {
  98. obj.attachEvent("on" + type, fn);
  99. } else {
  100. obj["on" + type] = fn;
  101. }
  102. };
  103. //阻断mouseup
  104. jeDt.stopMosup = function(evt, elem) {
  105. if (evt !== "mouseup") {
  106. jeDt.on(elem, "mouseup", function(ev) {
  107. jeDt.stopmp(ev);
  108. });
  109. }
  110. };
  111. jeDt.html = function(elem, value) {
  112. if (typeof value != "undefined" || value !== undefined && elem.nodeType === 1) {
  113. elem.innerHTML = value;
  114. } else {
  115. return elem.innerHTML;
  116. }
  117. return this;
  118. };
  119. jeDt.text = function(elem, value) {
  120. if (value !== undefined && elem.nodeType === 1) {
  121. document.all ? elem.innerText = value :elem.textContent = value;
  122. } else {
  123. var emText = document.all ? elem.innerText :elem.textContent;
  124. return emText;
  125. }
  126. return this;
  127. };
  128. jeDt.val = function(elem, value) {
  129. if (value !== undefined && elem.nodeType === 1) {
  130. elem.value = value;
  131. } else {
  132. return elem.value;
  133. }
  134. return this;
  135. };
  136. jeDt.scroll = function(type) {
  137. type = type ? "scrollLeft" :"scrollTop";
  138. return doc.body[type] | doc.documentElement[type];
  139. };
  140. jeDt.winarea = function(type) {
  141. return doc.documentElement[type ? "clientWidth" :"clientHeight"];
  142. };
  143. //转换日期格式
  144. jeDt.parse = function(ymd, hms, format) {
  145. ymd = ymd.concat(hms);
  146. var format = format, _this = this;
  147. return format.replace(/YYYY|MM|DD|hh|mm|ss/g, function(str, index) {
  148. ymd.index = ++ymd.index | 0;
  149. return jeDt.digit(ymd[ymd.index]);
  150. });
  151. };
  152. //初始化日期
  153. jeDt.nowDate = function(timestamp, format) {
  154. var De = new Date(timestamp | 0 ? function(tamp) {
  155. return tamp < 864e5 ? +new Date() + tamp * 864e5 :tamp;
  156. }(parseInt(timestamp)) :+new Date());
  157. return jeDt.parse([ De.getFullYear(), De.getMonth() + 1, De.getDate() ], [ De.getHours(), De.getMinutes(), De.getSeconds() ], format);
  158. };
  159. jeDt.montharr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ];
  160. //判断元素类型
  161. jeDt.isValHtml = function(that) {
  162. return /textarea|input/.test(that.tagName.toLocaleLowerCase());
  163. };
  164. jeDt.weeks = [ "日", "一", "二", "三", "四", "五", "六" ];
  165. //节日
  166. jeDt.festival = function(md, n) {
  167. var str = "";
  168. switch (md) {
  169. case "01.01": str = "元旦"; break;
  170. case "02.14": str = "情人"; break;
  171. case "03.08": str = "妇女"; break;
  172. case "04.05": str = "清明"; break;
  173. case "05.01": str = "劳动"; break;
  174. case "06.01": str = "儿童"; break;
  175. case "08.01": str = "建军"; break;
  176. case "09.10": str = "教师"; break;
  177. case "10.01": str = "国庆"; break;
  178. case "12.24": str = "平安"; break;
  179. case "12.25": str = "圣诞"; break;
  180. default: str = n; break;
  181. }
  182. return str;
  183. };
  184. //补齐数位
  185. jeDt.digit = function(num) {
  186. return num < 10 ? "0" + (num | 0) :num;
  187. };
  188. //显示隐藏层
  189. jeDt.shdeCell = function(type) {
  190. type ? QD(Cell)[0].style.display = "none" :QD(Cell)[0].style.display = "block";
  191. };
  192. var config = {
  193. dateCell:"#dateval",
  194. format:"YYYY-MM-DD hh:mm:ss", //日期格式
  195. minDate:"1900-01-01 00:00:00", //最小日期
  196. maxDate:"2099-12-31 23:59:59", //最大日期
  197. isinitVal:false, //是否初始化时间
  198. isTime:false, //是否开启时间选择
  199. isClear:true, //是否显示清空
  200. festival:false, //是否显示节日
  201. zIndex:999, //弹出层的层级高度
  202. choosefun:function(val) {},
  203. clearfun:function(val) {},
  204. okfun:function(val) {}
  205. }, InitDate = function(options) {
  206. var that = this, newConf = JSON.parse(JSON.stringify(config));
  207. that.config = jeDt.extend(newConf, options);
  208. that.init();
  209. };
  210. var jeDate = function(options) {
  211. return new InitDate(options || {});
  212. };
  213. InitDate.prototype = {
  214. init:function() {
  215. var that = this, opts = that.config, self = QD(opts.dateCell)[0], elem, devt, even = window.event, target;
  216. var dateDiv = doc.createElement("div");
  217. if (!QD(Cell)[0]) {
  218. dateDiv.className = dateDiv.id = Cell.replace("#", "");
  219. dateDiv.style.zIndex = opts.zIndex;
  220. doc.body.appendChild(dateDiv);
  221. }
  222. try {
  223. target = even.target || even.srcElement || {};
  224. } catch (e) {
  225. target = {};
  226. }
  227. elem = opts.dateCell ? QD(opts.dateCell)[0] :target;
  228. var nowDateVal = jeDt.nowDate(null, opts.format);
  229. if (opts.isinitVal) {
  230. (jeDt.val(self) || jeDt.text(self)) == "" ? jeDt.isValHtml(self) ? jeDt.val(self, nowDateVal) :jeDt.text(self, nowDateVal) :jeDt.isValHtml(self) ? jeDt.val(self) :jeDt.text(self);
  231. }
  232. if (even && target.tagName) {
  233. if (!elem || elem === jeDt.elem) return;
  234. jeDt.stopMosup(even.type, elem);
  235. jeDt.stopmp(even);
  236. that.setHtml(opts, self);
  237. } else {
  238. devt = opts.event || "click";
  239. jeDt.each((elem.length | 0) > 0 ? elem :[ elem ], function(ii, cel) {
  240. jeDt.stopMosup(devt, that);
  241. jeDt.on(cel, devt, function(ev) {
  242. jeDt.stopmp(ev);
  243. if (cel !== jeDt.elem) that.setHtml(opts, self);
  244. });
  245. });
  246. }
  247. },
  248. setHtml:function(opts, self) {
  249. var that = this, weekHtml = "", date = new Date(), nowDateVal = jeDt.nowDate(null, opts.format), isformat = opts.format.match(/\w+|d+/g).join("-") == "YYYY-MM" ? true :false;
  250. var initVal = opts.isinitVal ? jeDt.isValHtml(self) ? jeDt.val(self) :jeDt.text(self) :(jeDt.val(self) || jeDt.text(self)) == "" ? nowDateVal :jeDt.isValHtml(self) ? jeDt.val(self) :jeDt.text(self);
  251. if (jeDt.val(self) != "" || jeDt.text(self) != "") {
  252. var arrTime = initVal.match(/\d+/g);
  253. } else {
  254. var arrTime = [ date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() ];
  255. }
  256. var topymSty = !isformat ? '<div class="jedateym" style="width:50%;"><i class="prev triangle yearprev"></i><span class="jedateyy" data-ym="24"><em class="jedateyear"></em><em class="pndrop"></em></span><i class="next triangle yearnext"></i></div>' + '<div class="jedateym" style="width:50%;"><i class="prev triangle monthprev"></i><span class="jedatemm" data-ym="12"><em class="jedatemonth"></em><em class="pndrop"></em></span><i class="next triangle monthnext"></i></div>' :'<div class="jedateym" style="width:100%;"><i class="prev triangle ymprev"></i><span class="jedateyy"><em class="jedateyearmonth"></em></span><i class="next triangle ymnext"></i></div>';
  257. var datetopStr = '<div class="jedatetop">' + topymSty + "</div>";
  258. var dateymList = !isformat ? '<div class="jedatetopym" style="display: none;">' + '<ul class="ymdropul"></ul><p><span class="jedateymchle">&#8592;</span><span class="jedateymchri">&#8594;</span><span class="jedateymchok">关闭</span></p>' + "</div>" :'<ul class="jedaym"></ul>';
  259. var dateriList = '<ol class="jedaol"></ol><ul class="jedaul"></ul>';
  260. var bothmsStr = !isformat ? '<ul class="botflex jedatehms"><li><em data-hms="24"></em><i>:</i></li><li><em data-hms="60"></em><i>:</i></li><li><em data-hms="60"></em></li></ul>' + '<div class="botflex jedatebtn"><span class="jedateclear" style="width:31%;">清空</span><span class="jedatetodaymonth" style="width:31%;">今天</span><span class="jedateok" style="width:31%;">确认</span></div>' :'<div class="botflex jedatebtn"><span class="jedateclear" style="width:31%;">清空</span><span class="jedatetodaymonth" style="width:31%;">本月</span><span class="jedateok" style="width:31%;">确认</span></div>';
  261. var datebotStr = '<div class="jedatebot">' + bothmsStr + "</div>";
  262. var dateHtmStr = isformat ? datetopStr + dateymList + datebotStr :datetopStr + dateymList + dateriList + datebotStr + '<div class="jedateprophms"></div>';
  263. jeDt.html(QD(Cell)[0], dateHtmStr);
  264. opts.isClear ? "" :QD(Cell + " .jedatebot .jedateclear")[0].style.display = "none";
  265. if (opts.isTime) {
  266. var dhmsArr = jeDt.val(self) != "" || jeDt.text(self) != "" ? [ arrTime[3], arrTime[4], arrTime[5] ] :[ date.getHours(), date.getMinutes() + 1, date.getSeconds() ];
  267. jeDt.each(QD(Cell + " .jedatebot .jedatehms em"), function(i, cls) {
  268. jeDt.html(cls, jeDt.digit(dhmsArr[i]));
  269. });
  270. } else {
  271. if (!isformat) QD(Cell + " .jedatebot .jedatehms")[0].style.display = "none";
  272. QD(Cell + " .jedatebot .jedatebtn")[0].style.width = "100%";
  273. }
  274. if (!isformat) {
  275. for (var i = 0; i < jeDt.weeks.length; i++) {
  276. weekHtml += '<li class="weeks" data-week="' + jeDt.weeks[i] + '">' + jeDt.weeks[i] + "</li>";
  277. }
  278. jeDt.html(QD(Cell + " .jedaol")[0], weekHtml);
  279. that.getDateStr(arrTime[0], arrTime[1], arrTime[2]);
  280. that.YearAndMonth(that, opts, self, arrTime);
  281. } else {
  282. jeDt.html(QD(Cell + " .jedaym")[0], that.onlyYMStr(arrTime[0], arrTime[1]));
  283. jeDt.text(QD(Cell + " .jedateym .jedateyearmonth")[0], arrTime[0] + "年" + jeDt.digit(arrTime[1]) + "月");
  284. that.onlyYMevents(that, opts, self, arrTime);
  285. }
  286. jeDt.shdeCell(false);
  287. that.orien(QD(Cell)[0], self);
  288. that.events(that, opts, self, arrTime);
  289. },
  290. onlyYMStr:function(y, m) {
  291. var onlyYM = "";
  292. jeDt.each(jeDt.montharr, function(i, val) {
  293. onlyYM += "<li " + (m == val ? 'class="action"' :"") + ' data-onym="' + y + "-" + jeDt.digit(val) + '">' + y + "年" + jeDt.digit(val) + "月</li>";
  294. });
  295. return onlyYM;
  296. },
  297. onlyYMevents:function(that, opts, self, arrTime) {
  298. var ymPre = QD(Cell + " .jedateym .ymprev")[0], ymNext = QD(Cell + " .jedateym .ymnext")[0], ony = parseInt(arrTime[0]), onm = parseInt(arrTime[1]);
  299. jeDt.each([ ymPre, ymNext ], function(i, cls) {
  300. jeDt.on(cls, "click", function(ev) {
  301. jeDt.stopmp(ev);
  302. var ym = cls == ymPre ? ony -= 1 :ony += 1;
  303. jeDt.html(QD(Cell + " .jedaym")[0], that.onlyYMStr(ym, onm));
  304. that.events(that, opts, self, arrTime);
  305. });
  306. });
  307. },
  308. //方位辨别
  309. orien:function(obj, self, pos) {
  310. var tops, rect = self.getBoundingClientRect();
  311. obj.style.left = rect.left + (pos ? 0 :jeDt.scroll(1)) + "px";
  312. tops = (rect.bottom + obj.offsetHeight / 1.5 <= jeDt.winarea()) ?
  313. rect.bottom - 1 : rect.top > obj.offsetHeight / 1.5 ? rect.top - obj.offsetHeight + 1 :jeDt.winarea() - obj.offsetHeight;
  314. obj.style.top = Math.max(tops + (pos ? 0 :jeDt.scroll()) + 1, 1) + "px";
  315. },
  316. getDateStr:function(y, m, d) {
  317. var that = this, opts = that.config, dayStr = "", m = jeDt.digit(m);
  318. jeDt.text(QD(Cell + " .jedateyear")[0], y + "年").attr(QD(Cell + " .jedateyear")[0], "data-year", y);
  319. jeDt.text(QD(Cell + " .jedatemonth")[0], m + "月").attr(QD(Cell + " .jedatemonth")[0], "data-month", m);
  320. //是否显示节日
  321. var isfestival = function(day, n) {
  322. return opts.festival ? jeDt.festival(day, n) :n;
  323. };
  324. var parseArr = function(str) {
  325. var timeArr = str.split(" ");
  326. return timeArr[0].split("-");
  327. };
  328. //先得到当前月第一天是星期几.
  329. var date = setMonthDays(y, m), weekday = new Date(y, parseInt(m) - 1, 1).getDay();
  330. //根据这个星期算前面几天的上个月最后几天.
  331. var pervLastDay = weekday != 0 ? weekday :weekday + 7;
  332. //得到上个月最后一天;
  333. var pervMonthlastDay = getPervMonthLastDay(y, m), currentMonthDays = getPervMonthLastDay(y, parseInt(m) + 1);
  334. //上月最后几天循环
  335. var lastdays = pervMonthlastDay - pervLastDay;
  336. //判断是否超出允许的日期范围
  337. var startDay = 1, minArr = parseArr(opts.minDate), maxArr = parseArr(opts.maxDate), endDay = currentMonthDays, thisDate = new Date(y, m, d), firstDate = new Date(y, m, 1), lastDate = new Date(y, m, currentMonthDays), minTime = new Date(minArr[0], minArr[1], minArr[2]), maxTime = new Date(maxArr[0], maxArr[1], maxArr[2]), minDateDay = minTime.getDate();
  338. if (minTime > lastDate) {
  339. startDay = parseInt(currentMonthDays) + 1;
  340. } else if (minTime >= firstDate && minTime <= lastDate) {
  341. startDay = minDateDay;
  342. } else if (minTime >= firstDate) {}
  343. if (maxTime) {
  344. var maxDateDay = maxTime.getDate();
  345. if (maxTime < firstDate) {
  346. endDay = startDay;
  347. } else if (maxTime >= firstDate && maxTime <= lastDate) {
  348. endDay = maxDateDay;
  349. }
  350. }
  351. //循环上月剩余的天数
  352. for (var p = pervLastDay - 1; p >= 0; p--) {
  353. var py, pm, preCls, preDays = jeDt.digit(pervMonthlastDay - p);
  354. m == 1 ? (py = parseInt(y) - 1, pm = 13) :(py = y, pm = m);
  355. var thatpretm = parseInt(py.toString() + jeDt.digit(parseInt(pm) - 1).toString() + preDays.toString()), minpretm = parseInt(minArr[0].toString() + jeDt.digit(minArr[1]).toString() + jeDt.digit(minArr[2]).toString()), maxnexttm = parseInt(maxArr[0].toString() + jeDt.digit(maxArr[1]).toString() + jeDt.digit(maxArr[2]).toString());
  356. preCls = thatpretm >= minpretm && thatpretm <= maxnexttm ? "prevdate" :preCls = "disabled";
  357. dayStr += "<li class='" + preCls + "' data-y='" + py + "' data-m='" + (parseInt(pm) - 1) + "' data-d='" + preDays + "'>" + isfestival(parseInt(pm) - 1 + "." + preDays, preDays) + "</li>";
  358. }
  359. //循环本月的天数,将日期按允许的范围分三段拼接
  360. for (var i = 1; i < startDay; i++) {
  361. i = jeDt.digit(i);
  362. dayStr += '<li class="disabled" data-y="' + y + '" data-m="' + m + '" data-d="' + i + '">' + isfestival(m + "." + i, i) + "</li>";
  363. }
  364. for (var j = startDay; j <= endDay; j++) {
  365. var current = "";
  366. j = jeDt.digit(j);
  367. if (/*y==value.year && m==value.month+1&& */ d == j) {
  368. current = "action";
  369. }
  370. dayStr += '<li class="' + current + '" data-y="' + y + '" data-m="' + m + '" data-d="' + j + '">' + isfestival(m + "." + j, j) + "</li>";
  371. }
  372. for (var k = endDay + 1; k <= currentMonthDays; k++) {
  373. k = jeDt.digit(k);
  374. dayStr += '<li class="disabled" data-y="' + y + '" data-m="' + m + '" data-d="' + k + '">' + isfestival(m + "." + k, k) + "</li>";
  375. }
  376. //循环补上下个月的开始几天
  377. var nextDayArr = [], nextMonthStartDays = 42 - pervLastDay - setMonthDays(y, m);
  378. for (var n = 1; n <= nextMonthStartDays; n++) {
  379. var ny, nm, nextCls;
  380. n = jeDt.digit(n);
  381. m >= 12 ? (ny = parseInt(y) + 1, nm = 0) :(ny = y, nm = m);
  382. var thatnexttm = parseInt(ny.toString() + jeDt.digit(parseInt(nm) + 1).toString() + jeDt.digit(n).toString()), minnexttm = parseInt(minArr[0].toString() + jeDt.digit(minArr[1]).toString() + jeDt.digit(minArr[2]).toString()), maxnexttm = parseInt(maxArr[0].toString() + jeDt.digit(maxArr[1]).toString() + jeDt.digit(maxArr[2]).toString());
  383. nextCls = thatnexttm <= maxnexttm && thatnexttm >= minnexttm ? "nextdate" :nextCls = "disabled";
  384. dayStr += "<li class='" + nextCls + "' data-y='" + ny + "' data-m='" + (parseInt(nm) + 1) + "' data-d='" + n + "'>" + isfestival(parseInt(nm) + 1 + "." + n, n) + "</li>";
  385. }
  386. jeDt.html(QD(Cell + " .jedaul")[0], dayStr);
  387. jeDt.attr(QD(Cell + " .monthprev")[0], "data-y", jeDt.digit(parseInt(m) - 1));
  388. jeDt.attr(QD(Cell + " .monthnext")[0], "data-y", jeDt.digit(parseInt(m) + 1));
  389. //计算某年某月有多少天,如果是二月,闰年28天否则29天
  390. function setMonthDays(year, month) {
  391. var er = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 :28;
  392. return [ 31, er, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ][month - 1];
  393. }
  394. //得到指定月的上个月最后一天传进来按 12月算
  395. function getPervMonthLastDay(year, month) {
  396. //当月就是 yue-1 也就是计算机里面的0-11月份,那么算上个月的最后一天就是当月的0天
  397. return parseInt(new Date(year, month - 1, 0).getDate());
  398. }
  399. },
  400. events:function(that, opts, self, arrTime) {
  401. var yearPre = QD(Cell + " .yearprev")[0], yearNext = QD(Cell + " .yearnext")[0], monthPre = QD(Cell + " .monthprev")[0], monthNext = QD(Cell + " .monthnext")[0], newDate = new Date(), jedateyear = QD(Cell + " .jedateyear")[0], jedatemonth = QD(Cell + " .jedatemonth")[0], isformat = opts.format.match(/\w+|d+/g).join("-") == "YYYY-MM" ? true :false;
  402. if (!isformat) {
  403. //切换年
  404. jeDt.each([ yearPre, yearNext ], function(i, cls) {
  405. jeDt.on(cls, "click", function(ev) {
  406. jeDt.stopmp(ev);
  407. var y = parseInt(jeDt.attr(jedateyear, "data-year")), m = parseInt(jeDt.attr(jedatemonth, "data-month"));
  408. cls == yearPre ? y -= 1 :y += 1;
  409. var d = newDate.toLocaleDateString() == y + "/" + m + "/" + newDate.getDate() ? arrTime[2] :1;
  410. that.getDateStr(y, m, d);
  411. that.clickLiDays(that, opts, self);
  412. });
  413. });
  414. //切换月
  415. jeDt.each([ monthPre, monthNext ], function(i, cls) {
  416. jeDt.on(cls, "click", function(ev) {
  417. jeDt.stopmp(ev);
  418. var y = parseInt(jeDt.attr(jedateyear, "data-year")), m = parseInt(jeDt.attr(jedatemonth, "data-month"));
  419. if (cls == monthPre) {
  420. m == 1 ? (y -= 1, m = 12) :m -= 1;
  421. } else {
  422. m == 12 ? (y += 1, m = 1) :m += 1;
  423. }
  424. var d = newDate.toLocaleDateString() == y + "/" + m + "/" + newDate.getDate() ? arrTime[2] :1;
  425. that.getDateStr(y, m, d);
  426. that.clickLiDays(that, opts, self);
  427. });
  428. });
  429. //生成定位时分秒
  430. jeDt.each(QD(Cell + " .jedatebot .jedatehms em"), function(i, cls) {
  431. jeDt.on(cls, "click", function() {
  432. var hmsStr = "", acton, hmscell = QD(Cell + " .jedateprophms")[0], hmslen = jeDt.attr(cls, "data-hms"), hmsstxt = [ "小时", "分钟", "秒数" ], removeEmpty = function() {
  433. jeDt.removeClass(hmscell, hmslen == 24 ? "jedateh" :"jedatems");
  434. jeDt.html(hmscell, "");
  435. };
  436. hmsStr += '<div class="jedatehmstitle">' + hmsstxt[i] + '<div class="jedatehmsclose">&times;</div></div>';
  437. for (var h = 0; h < hmslen; h++) {
  438. h = jeDt.digit(h);
  439. acton = jeDt.text(cls) == h ? "action" :"";
  440. hmsStr += '<p class="' + acton + '">' + h + "</p>";
  441. }
  442. jeDt.removeClass(hmscell, hmslen == 24 ? "jedatems" :"jedateh").addClass(hmscell, hmslen == 24 ? "jedateh" :"jedatems");
  443. jeDt.html(hmscell, hmsStr);
  444. jeDt.each(QD(Cell + " .jedateprophms p"), function(i, p) {
  445. jeDt.on(p, "click", function() {
  446. jeDt.html(cls, jeDt.digit(jeDt.text(p)));
  447. removeEmpty();
  448. });
  449. });
  450. jeDt.each(QD(Cell + " .jedateprophms .jedatehmstitle"), function(i, c) {
  451. jeDt.on(c, "click", function() {
  452. removeEmpty();
  453. });
  454. });
  455. });
  456. });
  457. //今天
  458. jeDt.on(QD(Cell + " .jedatebot .jedatetodaymonth")[0], "click", function() {
  459. var toTime = [ newDate.getFullYear(), newDate.getMonth() + 1, newDate.getDate(), newDate.getHours(), newDate.getMinutes(), newDate.getSeconds() ];
  460. var gettoDate = jeDt.parse([ toTime[0], toTime[1], toTime[2] ], [ toTime[3], toTime[4], toTime[5] ], opts.format);
  461. that.getDateStr(toTime[0], toTime[1], toTime[2]);
  462. jeDt.isValHtml(self) ? jeDt.val(self, gettoDate) :jeDt.text(self, gettoDate);
  463. jeDt.html(QD(Cell)[0], "");
  464. jeDt.shdeCell(true);
  465. if (opts.choosefun === "function" || opts.choosefun != null) opts.choosefun(gettoDate);
  466. });
  467. } else {
  468. jeDt.each(QD(Cell + " .jedaym li"), function(i, cls) {
  469. jeDt.on(cls, "click", function(ev) {
  470. jeDt.stopmp(ev);
  471. var atYM = jeDt.attr(cls, "data-onym").match(/\w+|d+/g);
  472. var getYMDate = jeDt.parse([ atYM[0], atYM[1], 1 ], [ 0, 0, 0 ], opts.format);
  473. jeDt.isValHtml(self) ? jeDt.val(self, getYMDate) :jeDt.text(self, getYMDate);
  474. jeDt.html(QD(Cell)[0], "");
  475. jeDt.shdeCell(true);
  476. });
  477. });
  478. //本月
  479. jeDt.on(QD(Cell + " .jedatebot .jedatetodaymonth")[0], "click", function() {
  480. var thisYM = jeDt.attr(QD(Cell + " .jedaym .action")[0], "data-onym").match(/\w+|d+/g), thisYMDate = jeDt.parse([ thisYM[0], thisYM[1], 1 ], [ 0, 0, 0 ], opts.format);
  481. jeDt.isValHtml(self) ? jeDt.val(self, thisYMDate) :jeDt.text(self, thisYMDate);
  482. jeDt.html(QD(Cell)[0], "");
  483. jeDt.shdeCell(true);
  484. if (opts.choosefun === "function" || opts.choosefun != null) opts.choosefun(thisYMDate);
  485. });
  486. }
  487. //清空
  488. jeDt.on(QD(Cell + " .jedatebot .jedateclear")[0], "click", function() {
  489. var clearVal = jeDt.isValHtml(self) ? jeDt.val(self) :jeDt.text(self);
  490. jeDt.isValHtml(self) ? jeDt.val(self, "") :jeDt.text(self, "");
  491. jeDt.html(QD(Cell)[0], "");
  492. jeDt.shdeCell(true);
  493. if(clearVal != ""){
  494. if (opts.clearfun === "function" || opts.clearfun != null) opts.clearfun(clearVal);
  495. }
  496. });
  497. //确认
  498. jeDt.on(QD(Cell + " .jedatebot .jedateok")[0], "click", function(ev) {
  499. jeDt.stopmp(ev);
  500. var jedCell = isformat ? QD(Cell + " .jedaym li") :QD(Cell + " .jedaul li")
  501. if (!isformat) {
  502. var okTimeArr = [], okTime = [ parseInt(jeDt.attr(jedateyear, "data-year")), parseInt(jeDt.attr(jedatemonth, "data-month")), arrTime[2] ];
  503. jeDt.each(QD(Cell + " .jedatehms em"), function(l, tval) {
  504. okTimeArr.push(jeDt.text(tval));
  505. });
  506. var okVal = jeDt.val(self) != "" || jeDt.text(self) != "" ? jeDt.parse([ okTime[0], okTime[1], okTime[2] ], [ okTimeArr[0], okTimeArr[1], okTimeArr[2] ], opts.format) :"";
  507. that.getDateStr(okTime[0], okTime[1], okTime[2]);
  508. }else{
  509. var jedYM = jeDt.val(self) != "" || jeDt.text(self) != "" ? jeDt.attr(QD(Cell + " .jedaym .action")[0], "data-onym").match(/\w+|d+/g) :"",
  510. okVal = jeDt.val(self) != "" || jeDt.text(self) != "" ? jeDt.parse([ jedYM[0], jedYM[1], 1 ], [ 0, 0, 0 ], opts.format) :""
  511. }
  512. jeDt.each(jedCell, function(i, cls) {
  513. if (jeDt.attr(cls, "class") == "action") {
  514. jeDt.isValHtml(self) ? jeDt.val(self, okVal) :jeDt.text(self, okVal);
  515. }
  516. });
  517. jeDt.html(QD(Cell)[0], "");
  518. jeDt.shdeCell(true);
  519. if(okVal != ""){
  520. if (opts.okfun === "function" || opts.okfun != null) opts.okfun(okVal);
  521. }
  522. });
  523. //点击空白处隐藏
  524. jeDt.on(document, "click", function() {
  525. jeDt.shdeCell(true);
  526. jeDt.html(QD(Cell)[0], "");
  527. });
  528. jeDt.on(QD(Cell)[0], "click", function(ev) {
  529. jeDt.stopmp(ev);
  530. });
  531. that.clickLiDays(that, opts, self);
  532. },
  533. //下拉选择年和月
  534. YearAndMonth:function(that, opts, self, arrTime) {
  535. var jetopym = QD(Cell + " .jedatetopym")[0], jedateyy = QD(Cell + " .jedateyy")[0], jedatemm = QD(Cell + " .jedatemm")[0], jedateyear = QD(Cell + " .jedateyy .jedateyear")[0], jedatemonth = QD(Cell + " .jedatemm .jedatemonth")[0], mchri = QD(Cell + " .jedateymchri")[0], mchle = QD(Cell + " .jedateymchle")[0];
  536. function eachYears(YY) {
  537. var eachStr = "";
  538. jeDt.each(new Array(15), function(i) {
  539. if (i === 7) {
  540. var getyear = jeDt.attr(jedateyear, "data-year");
  541. eachStr += "<li " + (getyear == YY ? 'class="action"' :"") + ' data-y="' + YY + '">' + YY + "年</li>";
  542. } else {
  543. eachStr += '<li data-y="' + (YY - 7 + i) + '">' + (YY - 7 + i) + "年</li>";
  544. }
  545. });
  546. return eachStr;
  547. }
  548. function setYearMonth(YY, ymlen) {
  549. var ymStr = "";
  550. if (ymlen == 12) {
  551. jeDt.each(jeDt.montharr, function(i, val) {
  552. var getmonth = jeDt.attr(jedatemonth, "data-month"), val = jeDt.digit(val);
  553. ymStr += "<li " + (getmonth == val ? 'class="action"' :"") + ' data-m="' + val + '">' + val + "月</li>";
  554. });
  555. jeDt.each([ mchri, mchle ], function(c, cls) {
  556. cls.style.display = "none";
  557. });
  558. } else {
  559. ymStr = eachYears(YY);
  560. jeDt.each([ mchri, mchle ], function(c, cls) {
  561. cls.style.display = "block";
  562. });
  563. }
  564. jeDt.removeClass(jetopym, ymlen == 12 ? "jedatesety" :"jedatesetm").addClass(jetopym, ymlen == 12 ? "jedatesetm" :"jedatesety");
  565. jeDt.html(QD(Cell + " .jedatetopym .ymdropul")[0], ymStr);
  566. jetopym.style.display = "block";
  567. }
  568. function clickLiYears(year) {
  569. jeDt.each(QD(Cell + " .ymdropul li"), function(i, cls) {
  570. jeDt.on(cls, "click", function(ev) {
  571. var Years = jeDt.attr(this, "data-y"), Months = jeDt.attr(jedatemonth, "data-month");
  572. jeDt.attr(year, "data-year", Years);
  573. jeDt.html(year, Years);
  574. jetopym.style.display = "none";
  575. that.getDateStr(Years, Months, arrTime[2]);
  576. that.clickLiDays(that, opts, self);
  577. });
  578. });
  579. }
  580. //下拉选择年
  581. jeDt.on(jedateyy, "click", function() {
  582. var YMlen = parseInt(jeDt.attr(jedateyy, "data-ym")), yearAttr = parseInt(jeDt.attr(jedateyear, "data-year"));
  583. setYearMonth(yearAttr, YMlen);
  584. clickLiYears(jedateyear);
  585. });
  586. //下拉选择月
  587. jeDt.on(jedatemm, "click", function() {
  588. var YMlen = parseInt(jeDt.attr(jedatemm, "data-ym")), yearAttr = parseInt(jeDt.attr(jedateyear, "data-year"));
  589. setYearMonth(yearAttr, YMlen);
  590. jeDt.each(QD(Cell + " .ymdropul li"), function(i, cls) {
  591. jeDt.on(cls, "click", function(ev) {
  592. var Years = jeDt.attr(jedateyear, "data-year"), Months = jeDt.attr(this, "data-m");
  593. jeDt.attr(jedatemonth, "data-month", Months);
  594. jeDt.html(jedatemonth, Months);
  595. jetopym.style.display = "none";
  596. that.getDateStr(Years, Months, arrTime[2]);
  597. that.clickLiDays(that, opts, self);
  598. });
  599. });
  600. });
  601. //关闭下拉选择
  602. jeDt.on(QD(Cell + " .jedateymchok")[0], "click", function(ev) {
  603. jeDt.stopmp(ev);
  604. jetopym.style.display = "none";
  605. });
  606. var yearMch = parseInt(jeDt.attr(jedateyear, "data-year"));
  607. jeDt.each([ mchle, mchri ], function(lr, cls) {
  608. jeDt.on(cls, "click", function(ev) {
  609. jeDt.stopmp(ev);
  610. lr == 0 ? yearMch -= 15 :yearMch += 15;
  611. var mchStr = eachYears(yearMch);
  612. jeDt.html(QD(Cell + " .jedatetopym .ymdropul")[0], mchStr);
  613. clickLiYears(jedateyear);
  614. });
  615. });
  616. },
  617. //选择日
  618. clickLiDays:function(that, opts, self) {
  619. jeDt.each(QD(Cell + " .jedaul li"), function(i, cls) {
  620. jeDt.on(cls, "click", function(ev) {
  621. if (jeDt.hasClass(cls, "disabled")) return;
  622. jeDt.stopmp(ev);
  623. var liTms = [];
  624. jeDt.each(QD(Cell + " .jedatehms em"), function(l, tval) {
  625. liTms.push(jeDt.text(tval));
  626. });
  627. var aty = parseInt(jeDt.attr(cls, "data-y")) | 0, atm = parseInt(jeDt.attr(cls, "data-m")) | 0, atd = parseInt(jeDt.attr(cls, "data-d")) | 0;
  628. var getParDate = jeDt.parse([ aty, atm, atd ], [ liTms[0], liTms[1], liTms[2] ], opts.format);
  629. that.getDateStr(aty, atm, atd);
  630. jeDt.isValHtml(self) ? jeDt.val(self, getParDate) :jeDt.text(self, getParDate);
  631. jeDt.html(QD(Cell)[0], "");
  632. jeDt.shdeCell(true);
  633. if (opts.choosefun === "function" || opts.choosefun != null) {
  634. opts.choosefun(getParDate);
  635. }
  636. });
  637. });
  638. }
  639. };
  640. jeDt.getPath = (function(){
  641. var js = document.scripts, jsPath = js[js.length - 1].src;
  642. return jsPath.substring(0, jsPath.lastIndexOf("/") + 1);
  643. }());
  644. jeDt.creatlink = function(lib){
  645. var link = document.createElement('link');
  646. link.type = 'text/css';
  647. link.rel = 'stylesheet';
  648. link.href = jeDt.getPath +'skin/'+ lib + '.css';
  649. link.id = 'jeDateSkin';
  650. QD('head')[0].appendChild(link);
  651. link = null;
  652. };
  653. jeDt.creatlink('jedate');
  654. jeDate.skin = function(lib){
  655. QD('#jeDateSkin')[0].parentNode.removeChild(QD('#jeDateSkin')[0]);
  656. jeDt.creatlink(lib);
  657. };
  658. //返回指定日期
  659. jeDate.now = function(num) {
  660. var dd = new Date();
  661. dd.setDate(dd.getDate() + num);
  662. var y = dd.getFullYear(), m = dd.getMonth() + 1, d = dd.getDate();
  663. return y + "-" + m + "-" + d;
  664. };
  665. "function" === typeof define ? define(function() {
  666. return jeDate;
  667. }) :window.jeDate = jeDate;
  668. })(window);