JavaScript 日期选择器 Pikaday

参考网址:

http://www.jq22.com/jquery-info7564

 

pikaday.css

复制代码
  1 @charset "UTF-8";
  2 
  3 /*!
  4  * Pikaday
  5  * Copyright © 2012 David Bushell | BSD & MIT license | http://dbushell.com/
  6  */
  7 
  8 .pika-single {
  9     z-index: 9999;
 10     display: block;
 11     position: relative;
 12     width: 177px;
 13     padding: 8px;
 14     color: #333;
 15     background: #fff;
 16     border: 1px solid #ccc;
 17     border-bottom-color: #bbb;
 18     font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
 19 }
 20 
 21 .pika-single.is-hidden {
 22     display: none;
 23 }
 24 
 25 .pika-single.is-bound {
 26     position: absolute;
 27     box-shadow: 0 5px 15px -5px rgba(0,0,0,.5);
 28 }
 29 
 30 .pika-title {
 31     position: relative;
 32     text-align: center;
 33 }
 34 
 35 .pika-label {
 36     display: inline-block;
 37     *display: inline;
 38     position: relative;
 39     z-index: 9999;
 40     overflow: hidden;
 41     margin: 0;
 42     padding: 5px 3px;
 43     font-size: 14px;
 44     line-height: 20px;
 45     font-weight: bold;
 46     background-color: #fff;
 47 }
 48 .pika-title select {
 49     cursor: pointer;
 50     position: absolute;
 51     z-index: 9998;
 52     margin: 0;
 53     left: 0;
 54     top: 5px;
 55     filter: alpha(opacity=0);
 56     opacity: 0;
 57 }
 58 
 59 .pika-prev,
 60 .pika-next {
 61     display: block;
 62     cursor: pointer;
 63     position: relative;
 64     outline: none;
 65     border: 0;
 66     padding: 0;
 67     width: 20px;
 68     height: 30px;
 69     background-color: transparent;
 70     background-position: center center;
 71     background-repeat: no-repeat;
 72     background-size: 75% 75%;
 73     white-space: nowrap;
 74     text-indent: 100%;
 75     overflow: hidden;
 76     opacity: .5;
 77     *position: absolute;
 78     *top: 0;
 79 }
 80 
 81 .pika-prev:hover,
 82 .pika-next:hover {
 83     opacity: 1;
 84 }
 85 
 86 .pika-prev,
 87 .is-rtl .pika-next {
 88     float: left;
 89     background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAYAAAAsEj5rAAAAUklEQVR42u3VMQoAIBADQf8Pgj+OD9hG2CtONJB2ymQkKe0HbwAP0xucDiQWARITIDEBEnMgMQ8S8+AqBIl6kKgHiXqQqAeJepBo/z38J/U0uAHlaBkBl9I4GwAAAABJRU5ErkJggg==');
 90     *left: 0;
 91 }
 92 
 93 .pika-next,
 94 .is-rtl .pika-prev {
 95     float: right;
 96     background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAYAAAAsEj5rAAAAU0lEQVR42u3VOwoAMAgE0dwfAnNjU26bYkBCFGwfiL9VVWoO+BJ4Gf3gtsEKKoFBNTCoCAYVwaAiGNQGMUHMkjGbgjk2mIONuXo0nC8XnCf1JXgArVIZAQh5TKYAAAAASUVORK5CYII=');
 97     *right: 0;
 98 }
 99 
100 .pika-prev.is-disabled,
101 .pika-next.is-disabled {
102     cursor: default;
103     opacity: .2;
104 }
105 
106 .pika-select {
107     display: inline-block;
108     *display: inline;
109 }
110 
111 .pika-table {
112     width: 100%;
113     border-collapse: collapse;
114     border-spacing: 0;
115     border: 0;
116 }
117 
118 .pika-table th,
119 .pika-table td {
120     width: 14.285714285714286%;
121 }
122 
123 .pika-table th {
124     color: #999;
125     font-size: 12px;
126     line-height: 25px;
127     font-weight: bold;
128     text-align: center;
129 }
130 
131 .pika-button {
132     cursor: pointer;
133     display: block;
134     outline: none;
135     border: 0;
136     margin: 0;
137     width: 100%;
138     padding: 5px;
139     color: #666;
140     font-size: 12px;
141     line-height: 15px;
142     text-align:center;
143     background: #f5f5f5;
144 }
145 
146 .is-today .pika-button {
147     color: #33aaff;
148     font-weight: bold;
149 }
150 
151 .is-selected .pika-button {
152     color: #fff;
153     font-weight: bold;
154     background: #33aaff;
155     box-shadow: inset 0 1px 3px #178fe5;
156     border-radius: 3px;
157 }
158 
159 .is-disabled .pika-button {
160     pointer-events: none;
161     cursor: default;
162     color: #999;
163     opacity: .3;
164 }
165 
166 .pika-button:hover {
167     color: #fff !important;
168     background: #ff8000 !important;
169     box-shadow: none !important;
170     border-radius: 3px !important;
171 }
View Code
复制代码

 

pikaday.min.js

1 (function (e, t) { "use strict"; var n; if (typeof exports == "object") { try { n = require("moment") } catch (r) { } module.exports = t(n) } else typeof define == "function" && define.amd ? define(function (e) { var r = "moment"; return n = e.defined && e.defined(r) ? e(r) : undefined, t(n) }) : e.Pikaday = t(e.moment) })(this, function (e) { "use strict"; var t = typeof e == "function", n = !!window.addEventListener, r = window.document, i = window.setTimeout, s = function (e, t, r, i) { n ? e.addEventListener(t, r, !!i) : e.attachEvent("on" + t, r) }, o = function (e, t, r, i) { n ? e.removeEventListener(t, r, !!i) : e.detachEvent("on" + t, r) }, u = function (e, t, n) { var i; r.createEvent ? (i = r.createEvent("HTMLEvents"), i.initEvent(t, !0, !1), i = y(i, n), e.dispatchEvent(i)) : r.createEventObject && (i = r.createEventObject(), i = y(i, n), e.fireEvent("on" + t, i)) }, a = function (e) { return e.trim ? e.trim() : e.replace(/^\s+|\s+$/g, "") }, f = function (e, t) { return (" " + e.className + " ").indexOf(" " + t + " ") !== -1 }, l = function (e, t) { f(e, t) || (e.className = e.className === "" ? t : e.className + " " + t) }, c = function (e, t) { e.className = a((" " + e.className + " ").replace(" " + t + " ", " ")) }, h = function (e) { return /Array/.test(Object.prototype.toString.call(e)) }, p = function (e) { return /Date/.test(Object.prototype.toString.call(e)) && !isNaN(e.getTime()) }, d = function (e) { return e % 4 === 0 && e % 100 !== 0 || e % 400 === 0 }, v = function (e, t) { return [31, d(e) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][t] }, m = function (e) { p(e) && e.setHours(0, 0, 0, 0) }, g = function (e, t) { return e.getTime() === t.getTime() }, y = function (e, t, n) { var r, i; for (r in t) { i = e[r] !== undefined; if (i && typeof t[r] == "object" && t[r].nodeName === undefined) p(t[r]) ? n && (e[r] = new Date(t[r].getTime())) : h(t[r]) ? n && (e[r] = t[r].slice(0)) : e[r] = y({}, t[r], n); else if (n || !i) e[r] = t[r] } return e }, b = { field: null, bound: undefined, format: "YYYY-MM-DD", defaultDate: null, setDefaultDate: !1, firstDay: 0, minDate: null, maxDate: null, yearRange: 10, minYear: 0, maxYear: 9999, minMonth: undefined, maxMonth: undefined, isRTL: !1, yearSuffix: "", showMonthAfterYear: !1, numberOfMonths: 1, i18n: { previousMonth: "Previous Month", nextMonth: "Next Month", months: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"], monthsShort: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"], weekdays: ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"], weekdaysShort: ["日", "一", "二", "三", "四", "五", "六"] }, onSelect: null, onOpen: null, onClose: null, onDraw: null }, w = function (e, t, n) { t += e.firstDay; while (t >= 7) t -= 7; return n ? e.i18n.weekdaysShort[t] : e.i18n.weekdays[t] }, E = function (e, t, n, r, i) { if (i) return '<td class="is-empty"></td>'; var s = []; return r && s.push("is-disabled"), n && s.push("is-today"), t && s.push("is-selected"), '<td data-day="' + e + '" class="' + s.join(" ") + '"><button class="pika-button" type="button">' + e + "</button>" + "</td>" }, S = function (e, t) { return "<tr>" + (t ? e.reverse() : e).join("") + "</tr>" }, x = function (e) { return "<tbody>" + e.join("") + "</tbody>" }, T = function (e) { var t, n = []; for (t = 0; t < 7; t++) n.push('<th scope="col"><abbr title="' + w(e, t) + '">' + w(e, t, !0) + "</abbr></th>"); return "<thead>" + (e.isRTL ? n.reverse() : n).join("") + "</thead>" }, N = function (e) { var t, n, r, i = e._o, s = e._m, o = e._y, u = o === i.minYear, a = o === i.maxYear, f = '<div class="pika-title">', l, c, p = !0, d = !0; for (r = [], t = 0; t < 12; t++) r.push('<option value="' + t + '"' + (t === s ? " selected" : "") + (u && t < i.minMonth || a && t > i.maxMonth ? "disabled" : "") + ">" + i.i18n.months[t] + "</option>"); l = '<div class="pika-label">' + i.i18n.months[s] + '<select class="pika-select pika-select-month">' + r.join("") + "</select></div>", h(i.yearRange) ? (t = i.yearRange[0], n = i.yearRange[1] + 1) : (t = o - i.yearRange, n = 1 + o + i.yearRange); for (r = []; t < n && t <= i.maxYear; t++) t >= i.minYear && r.push('<option value="' + t + '"' + (t === o ? " selected" : "") + ">" + t + "</option>"); return c = '<div class="pika-label">' + o + i.yearSuffix + '&nbsp;&nbsp;年<select class="pika-select pika-select-year">' + r.join("") + "</select></div>", i.showMonthAfterYear ? f += c + l : f += c + l, u && (s === 0 || i.minMonth >= s) && (p = !1), a && (s === 11 || i.maxMonth <= s) && (d = !1), f += '<button class="pika-prev' + (p ? "" : " is-disabled") + '" type="button">' + i.i18n.previousMonth + "</button>", f += '<button class="pika-next' + (d ? "" : " is-disabled") + '" type="button">' + i.i18n.nextMonth + "</button>", f += "</div>" }, C = function (e, t) { return '<table cellpadding="0" cellspacing="0" class="pika-table">' + T(e) + x(t) + "</table>" }, k = function (o) { var u = this, a = u.config(o); u._onMouseDown = function (e) { if (!u._v) return; e = e || window.event; var t = e.target || e.srcElement; if (!t) return; if (!f(t, "is-disabled")) { if (f(t, "pika-button") && !f(t, "is-empty")) { u.setDate(new Date(u._y, u._m, parseInt(t.innerHTML, 10))), a.bound && i(function () { u.hide() }, 100); return } f(t, "pika-prev") ? u.prevMonth() : f(t, "pika-next") && u.nextMonth() } if (!f(t, "pika-select")) { if (!e.preventDefault) return e.returnValue = !1, !1; e.preventDefault() } else u._c = !0 }, u._onChange = function (e) { e = e || window.event; var t = e.target || e.srcElement; if (!t) return; f(t, "pika-select-month") ? u.gotoMonth(t.value) : f(t, "pika-select-year") && u.gotoYear(t.value) }, u._onInputChange = function (n) { var r; if (n.firedBy === u) return; t ? (r = e(a.field.value, a.format), r = r && r.isValid() ? r.toDate() : null) : r = new Date(Date.parse(a.field.value)), u.setDate(p(r) ? r : null), u._v || u.show() }, u._onInputFocus = function () { u.show() }, u._onInputClick = function () { u.show() }, u._onInputBlur = function () { u._c || (u._b = i(function () { u.hide() }, 50)), u._c = !1 }, u._onClick = function (e) { e = e || window.event; var t = e.target || e.srcElement, r = t; if (!t) return; !n && f(t, "pika-select") && (t.onchange || (t.setAttribute("onchange", "return;"), s(t, "change", u._onChange))); do if (f(r, "pika-single")) return; while (r = r.parentNode); u._v && t !== a.trigger && u.hide() }, u.el = r.createElement("div"), u.el.className = "pika-single" + (a.isRTL ? " is-rtl" : ""), s(u.el, "mousedown", u._onMouseDown, !0), s(u.el, "change", u._onChange), a.field && (a.bound ? r.body.appendChild(u.el) : a.field.parentNode.insertBefore(u.el, a.field.nextSibling), s(a.field, "change", u._onInputChange), a.defaultDate || (t && a.field.value ? a.defaultDate = e(a.field.value, a.format).toDate() : a.defaultDate = new Date(Date.parse(a.field.value)), a.setDefaultDate = !0)); var l = a.defaultDate; p(l) ? a.setDefaultDate ? u.setDate(l, !0) : u.gotoDate(l) : u.gotoDate(new Date), a.bound ? (this.hide(), u.el.className += " is-bound", s(a.trigger, "click", u._onInputClick), s(a.trigger, "focus", u._onInputFocus), s(a.trigger, "blur", u._onInputBlur)) : this.show() }; return k.prototype = { config: function (e) { this._o || (this._o = y({}, b, !0)); var t = y(this._o, e, !0); t.isRTL = !!t.isRTL, t.field = t.field && t.field.nodeName ? t.field : null, t.bound = !!(t.bound !== undefined ? t.field && t.bound : t.field), t.trigger = t.trigger && t.trigger.nodeName ? t.trigger : t.field; var n = parseInt(t.numberOfMonths, 10) || 1; t.numberOfMonths = n > 4 ? 4 : n, p(t.minDate) || (t.minDate = !1), p(t.maxDate) || (t.maxDate = !1), t.minDate && t.maxDate && t.maxDate < t.minDate && (t.maxDate = t.minDate = !1), t.minDate && (m(t.minDate), t.minYear = t.minDate.getFullYear(), t.minMonth = t.minDate.getMonth()), t.maxDate && (m(t.maxDate), t.maxYear = t.maxDate.getFullYear(), t.maxMonth = t.maxDate.getMonth()); if (h(t.yearRange)) { var r = (new Date).getFullYear() - 10; t.yearRange[0] = parseInt(t.yearRange[0], 10) || r, t.yearRange[1] = parseInt(t.yearRange[1], 10) || r } else t.yearRange = Math.abs(parseInt(t.yearRange, 10)) || b.yearRange, t.yearRange > 100 && (t.yearRange = 100); return t }, toString: function (e) { if (!p(this._d)) return ""; var n = this._d.getFullYear(), r = this._d.getMonth() + 1, i = this._d.getDate(); return r = r < 10 ? "0" + r : r, i = i < 10 ? "0" + i : i, p(this._d) ? t ? window.moment(this._d).format(e || this._o.format) : n + "-" + r + "-" + i : "" }, getMoment: function () { return t ? e(this._d) : null }, setMoment: function (n) { t && e.isMoment(n) && this.setDate(n.toDate()) }, getDate: function () { return p(this._d) ? new Date(this._d.getTime()) : null }, setDate: function (e, t) { if (!e) return this._d = null, this.draw(); typeof e == "string" && (e = new Date(Date.parse(e))); if (!p(e)) return; var n = this._o.minDate, r = this._o.maxDate; p(n) && e < n ? e = n : p(r) && e > r && (e = r), this._d = new Date(e.getTime()), m(this._d), this.gotoDate(this._d), this._o.field && (this._o.field.value = this.toString(), u(this._o.field, "change", { firedBy: this })), !t && typeof this._o.onSelect == "function" && this._o.onSelect.call(this, this.getDate()) }, gotoDate: function (e) { if (!p(e)) return; this._y = e.getFullYear(), this._m = e.getMonth(), this.draw() }, gotoToday: function () { this.gotoDate(new Date) }, gotoMonth: function (e) { isNaN(e = parseInt(e, 10)) || (this._m = e < 0 ? 0 : e > 11 ? 11 : e, this.draw()) }, nextMonth: function () { ++this._m > 11 && (this._m = 0, this._y++), this.draw() }, prevMonth: function () { --this._m < 0 && (this._m = 11, this._y--), this.draw() }, gotoYear: function (e) { isNaN(e) || (this._y = parseInt(e, 10), this.draw()) }, setMinDate: function (e) { this._o.minDate = e }, setMaxDate: function (e) { this._o.maxDate = e }, draw: function (e) { if (!this._v && !e) return; var t = this._o, n = t.minYear, r = t.maxYear, s = t.minMonth, o = t.maxMonth; this._y <= n && (this._y = n, !isNaN(s) && this._m < s && (this._m = s)), this._y >= r && (this._y = r, !isNaN(o) && this._m > o && (this._m = o)), this.el.innerHTML = N(this) + this.render(this._y, this._m), t.bound && (this.adjustPosition(), t.field.type !== "hidden" && i(function () { t.trigger.focus() }, 1)); if (typeof this._o.onDraw == "function") { var u = this; i(function () { u._o.onDraw.call(u) }, 0) } }, adjustPosition: function () { var e = this._o.trigger, t = e, n = this.el.offsetWidth, i = this.el.offsetHeight, s = window.innerWidth || r.documentElement.clientWidth, o = window.innerHeight || r.documentElement.clientHeight, u = window.pageYOffset || r.body.scrollTop || r.documentElement.scrollTop, a, f, l; if (typeof e.getBoundingClientRect == "function") l = e.getBoundingClientRect(), a = l.left + window.pageXOffset, f = l.bottom + window.pageYOffset; else { a = t.offsetLeft, f = t.offsetTop + t.offsetHeight; while (t = t.offsetParent) a += t.offsetLeft, f += t.offsetTop } a + n > s && (a = a - n + e.offsetWidth), f + i > o + u && (f = f - i - e.offsetHeight), this.el.style.cssText = "position:absolute;left:" + a + "px;top:" + f + "px;" }, render: function (e, t) { var n = this._o, r = new Date, i = v(e, t), s = (new Date(e, t, 1)).getDay(), o = [], u = []; m(r), n.firstDay > 0 && (s -= n.firstDay, s < 0 && (s += 7)); var a = i + s, f = a; while (f > 7) f -= 7; a += 7 - f; for (var l = 0, c = 0; l < a; l++) { var h = new Date(e, t, 1 + (l - s)), d = n.minDate && h < n.minDate || n.maxDate && h > n.maxDate, y = p(this._d) ? g(h, this._d) : !1, b = g(h, r), w = l < s || l >= i + s; u.push(E(1 + (l - s), y, b, d, w)), ++c === 7 && (o.push(S(u, n.isRTL)), u = [], c = 0) } return C(n, o) }, isVisible: function () { return this._v }, show: function () { this._v || (this._o.bound && s(r, "click", this._onClick), c(this.el, "is-hidden"), this._v = !0, this.draw(), typeof this._o.onOpen == "function" && this._o.onOpen.call(this)) }, hide: function () { var e = this._v; e !== !1 && (this._o.bound && o(r, "click", this._onClick), this.el.style.cssText = "", l(this.el, "is-hidden"), this._v = !1, e !== undefined && typeof this._o.onClose == "function" && this._o.onClose.call(this)) }, destroy: function () { this.hide(), o(this.el, "mousedown", this._onMouseDown, !0), o(this.el, "change", this._onChange), this._o.field && (o(this._o.field, "change", this._onInputChange), this._o.bound && (o(this._o.trigger, "click", this._onInputClick), o(this._o.trigger, "focus", this._onInputFocus), o(this._o.trigger, "blur", this._onInputBlur))), this.el.parentNode && this.el.parentNode.removeChild(this.el) } }, k })
View Code

 

引用:

<link href="Lib/CalendarPlug/pikaday.css" rel="stylesheet" />
<script src="Lib/CalendarPlug/pikaday.min.js"></script>

 

html

<input type="text" class="text-small" id="txt_ExperimentDate" style="width:178px;padding:7px 10px;border:1px solid #ccc;" />

 

js

            var picker = new Pikaday({
     field: document.getElementById('txt_ExperimentDate'),
     firstDay: 1,
     //minDate: new Date('2010-01-01'),
     //maxDate: new Date('2020-12-31'),
     yearRange: [2000, 2020],

 });

 

posted @   妖狐鬼魅  阅读(81)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示