bootstrap-datetimepicker.js 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /* =========================================================
  2. * bootstrap-datetimepicker.js
  3. * =========================================================
  4. * Copyright 2012 Stefan Petre
  5. * Improvements by Andrew Rowls
  6. * Improvements by Sébastien Malot
  7. * Project URL : http://www.malot.fr/bootstrap-datetimepicker
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. * ========================================================= */
  21. !function( $ ) {
  22. function UTCDate(){
  23. return new Date(Date.UTC.apply(Date, arguments));
  24. }
  25. function UTCToday(){
  26. var today = new Date();
  27. return UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate(), today.getUTCHours(), today.getUTCMinutes(), today.getUTCSeconds());
  28. }
  29. // Picker object
  30. var Datetimepicker = function(element, options) {
  31. var that = this;
  32. this.element = $(element);
  33. this.language = options.language || this.element.data('date-language') || "en";
  34. this.language = this.language in dates ? this.language : "en";
  35. this.isRTL = dates[this.language].rtl || false;
  36. this.formatType = options.formatType || this.element.data('format-type') || 'standard';
  37. this.format = DPGlobal.parseFormat(options.format || this.element.data('date-format') || DPGlobal.getDefaultFormat(this.formatType, 'input'), this.formatType);
  38. this.isInline = false;
  39. this.isVisible = false;
  40. this.isInput = this.element.is('input');
  41. this.component = this.element.is('.date') ? this.element.find('.add-on .icon-th, .add-on .icon-time, .add-on .icon-calendar').parent() : false;
  42. this.componentReset = this.element.is('.date') ? this.element.find('.add-on .icon-remove').parent() : false;
  43. this.hasInput = this.component && this.element.find('input').length;
  44. if (this.component && this.component.length === 0) {
  45. this.component = false;
  46. }
  47. this.linkField = options.linkField || this.element.data('link-field') || false;
  48. this.linkFormat = DPGlobal.parseFormat(options.linkFormat || this.element.data('link-format') || DPGlobal.getDefaultFormat(this.formatType, 'link'), this.formatType);
  49. this.minuteStep = options.minuteStep || this.element.data('minute-step') || 5;
  50. this.pickerPosition = options.pickerPosition || this.element.data('picker-position') || 'bottom-right';
  51. this.showMeridian = options.showMeridian || this.element.data('show-meridian') || false;
  52. this._attachEvents();
  53. this.minView = 0;
  54. if ('minView' in options) {
  55. this.minView = options.minView;
  56. } else if ('minView' in this.element.data()) {
  57. this.minView = this.element.data('min-view');
  58. }
  59. this.minView = DPGlobal.convertViewMode(this.minView);
  60. this.maxView = DPGlobal.modes.length-1;
  61. if ('maxView' in options) {
  62. this.maxView = options.maxView;
  63. } else if ('maxView' in this.element.data()) {
  64. this.maxView = this.element.data('max-view');
  65. }
  66. this.maxView = DPGlobal.convertViewMode(this.maxView);
  67. this.startViewMode = 2;
  68. if ('startView' in options) {
  69. this.startViewMode = options.startView;
  70. } else if ('startView' in this.element.data()) {
  71. this.startViewMode = this.element.data('start-view');
  72. }
  73. this.startViewMode = DPGlobal.convertViewMode(this.startViewMode);
  74. this.viewMode = this.startViewMode;
  75. this.viewSelect = this.minView;
  76. if ('viewSelect' in options) {
  77. this.viewSelect = options.viewSelect;
  78. } else if ('viewSelect' in this.element.data()) {
  79. this.viewSelect = this.element.data('view-select');
  80. }
  81. this.viewSelect = DPGlobal.convertViewMode(this.viewSelect);
  82. this.forceParse = true;
  83. if ('forceParse' in options) {
  84. this.forceParse = options.forceParse;
  85. } else if ('dateForceParse' in this.element.data()) {
  86. this.forceParse = this.element.data('date-force-parse');
  87. }
  88. this.picker = $(DPGlobal.template)
  89. .appendTo(this.isInline ? this.element : 'body')
  90. .on({
  91. click: $.proxy(this.click, this),
  92. mousedown: $.proxy(this.mousedown, this)
  93. });
  94. if (this.isInline) {
  95. this.picker.addClass('datetimepicker-inline');
  96. } else {
  97. if (this.component && this.pickerPosition == 'bottom-left') {
  98. this.picker.addClass('datetimepicker-dropdown-left dropdown-menu');
  99. } else {
  100. this.picker.addClass('datetimepicker-dropdown dropdown-menu');
  101. }
  102. }
  103. if (this.isRTL){
  104. this.picker.addClass('datetimepicker-rtl');
  105. this.picker.find('.prev i, .next i')
  106. .toggleClass('icon-arrow-left icon-arrow-right');
  107. }
  108. $(document).on('mousedown', function (e) {
  109. // Clicked outside the datetimepicker, hide it
  110. if ($(e.target).closest('.datetimepicker').length === 0) {
  111. that.hide();
  112. }
  113. });
  114. this.autoclose = false;
  115. if ('autoclose' in options) {
  116. this.autoclose = options.autoclose;
  117. } else if ('dateAutoclose' in this.element.data()) {
  118. this.autoclose = this.element.data('date-autoclose');
  119. }
  120. this.keyboardNavigation = true;
  121. if ('keyboardNavigation' in options) {
  122. this.keyboardNavigation = options.keyboardNavigation;
  123. } else if ('dateKeyboardNavigation' in this.element.data()) {
  124. this.keyboardNavigation = this.element.data('date-keyboard-navigation');
  125. }
  126. this.todayBtn = (options.todayBtn || this.element.data('date-today-btn') || false);
  127. this.todayHighlight = (options.todayHighlight || this.element.data('date-today-highlight') || false);
  128. this.weekStart = ((options.weekStart || this.element.data('date-weekstart') || dates[this.language].weekStart || 0) % 7);
  129. this.weekEnd = ((this.weekStart + 6) % 7);
  130. this.startDate = -Infinity;
  131. this.endDate = Infinity;
  132. this.daysOfWeekDisabled = [];
  133. this.setStartDate(options.startDate || this.element.data('date-startdate'));
  134. this.setEndDate(options.endDate || this.element.data('date-enddate'));
  135. this.setDaysOfWeekDisabled(options.daysOfWeekDisabled || this.element.data('date-days-of-week-disabled'));
  136. this.fillDow();
  137. this.fillMonths();
  138. this.update();
  139. this.showMode();
  140. if(this.isInline) {
  141. this.show();
  142. }
  143. };
  144. Datetimepicker.prototype = {
  145. constructor: Datetimepicker,
  146. _events: [],
  147. _attachEvents: function(){
  148. this._detachEvents();
  149. if (this.isInput) { // single input
  150. this._events = [
  151. [this.element, {
  152. focus: $.proxy(this.show, this),
  153. keyup: $.proxy(this.update, this),
  154. keydown: $.proxy(this.keydown, this)
  155. }]
  156. ];
  157. }
  158. else if (this.component && this.hasInput){ // component: input + button
  159. this._events = [
  160. // For components that are not readonly, allow keyboard nav
  161. [this.element.find('input'), {
  162. focus: $.proxy(this.show, this),
  163. keyup: $.proxy(this.update, this),
  164. keydown: $.proxy(this.keydown, this)
  165. }],
  166. [this.component, {
  167. click: $.proxy(this.show, this)
  168. }]
  169. ];
  170. if (this.componentReset) {
  171. this._events.push([
  172. this.componentReset,
  173. {click: $.proxy(this.reset, this)}
  174. ]);
  175. }
  176. }
  177. else if (this.element.is('div')) { // inline datetimepicker
  178. this.isInline = true;
  179. }
  180. else {
  181. this._events = [
  182. [this.element, {
  183. click: $.proxy(this.show, this)
  184. }]
  185. ];
  186. }
  187. for (var i=0, el, ev; i<this._events.length; i++){
  188. el = this._events[i][0];
  189. ev = this._events[i][1];
  190. el.on(ev);
  191. }
  192. },
  193. _detachEvents: function(){
  194. for (var i=0, el, ev; i<this._events.length; i++){
  195. el = this._events[i][0];
  196. ev = this._events[i][1];
  197. el.off(ev);
  198. }
  199. this._events = [];
  200. },
  201. show: function(e) {
  202. this.picker.show();
  203. this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
  204. if (this.forceParse) {
  205. this.update();
  206. }
  207. this.place();
  208. $(window).on('resize', $.proxy(this.place, this));
  209. if (e) {
  210. e.stopPropagation();
  211. e.preventDefault();
  212. }
  213. this.isVisible = true;
  214. this.element.trigger({
  215. type: 'show',
  216. date: this.date
  217. });
  218. },
  219. hide: function(e){
  220. if(!this.isVisible) return;
  221. if(this.isInline) return;
  222. this.picker.hide();
  223. $(window).off('resize', this.place);
  224. this.viewMode = this.startViewMode;
  225. this.showMode();
  226. if (!this.isInput) {
  227. $(document).off('mousedown', this.hide);
  228. }
  229. if (
  230. this.forceParse &&
  231. (
  232. this.isInput && this.element.val() ||
  233. this.hasInput && this.element.find('input').val()
  234. )
  235. )
  236. this.setValue();
  237. this.isVisible = false;
  238. this.element.trigger({
  239. type: 'hide',
  240. date: this.date
  241. });
  242. },
  243. remove: function() {
  244. this._detachEvents();
  245. this.picker.remove();
  246. delete this.element.data().datetimepicker;
  247. },
  248. getDate: function() {
  249. var d = this.getUTCDate();
  250. return new Date(d.getTime() + (d.getTimezoneOffset()*60000));
  251. },
  252. getUTCDate: function() {
  253. return this.date;
  254. },
  255. setDate: function(d) {
  256. this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
  257. },
  258. setUTCDate: function(d) {
  259. if (d >= this.startDate && d <= this.endDate) {
  260. this.date = d;
  261. this.setValue();
  262. this.viewDate = this.date;
  263. this.fill();
  264. } else {
  265. this.element.trigger({
  266. type: 'outOfRange',
  267. date: d,
  268. startDate: this.startDate,
  269. endDate: this.endDate
  270. });
  271. }
  272. },
  273. setValue: function() {
  274. var formatted = this.getFormattedDate();
  275. if (!this.isInput) {
  276. if (this.component){
  277. this.element.find('input').prop('value', formatted);
  278. }
  279. this.element.data('date', formatted);
  280. } else {
  281. this.element.prop('value', formatted);
  282. }
  283. if (this.linkField) {
  284. $('#' + this.linkField).val(this.getFormattedDate(this.linkFormat));
  285. }
  286. },
  287. getFormattedDate: function(format) {
  288. if(format == undefined) format = this.format;
  289. return DPGlobal.formatDate(this.date, format, this.language, this.formatType);
  290. },
  291. setStartDate: function(startDate){
  292. this.startDate = startDate || -Infinity;
  293. if (this.startDate !== -Infinity) {
  294. this.startDate = DPGlobal.parseDate(this.startDate, this.format, this.language, this.formatType);
  295. }
  296. this.update();
  297. this.updateNavArrows();
  298. },
  299. setEndDate: function(endDate){
  300. this.endDate = endDate || Infinity;
  301. if (this.endDate !== Infinity) {
  302. this.endDate = DPGlobal.parseDate(this.endDate, this.format, this.language, this.formatType);
  303. }
  304. this.update();
  305. this.updateNavArrows();
  306. },
  307. setDaysOfWeekDisabled: function(daysOfWeekDisabled){
  308. this.daysOfWeekDisabled = daysOfWeekDisabled || [];
  309. if (!$.isArray(this.daysOfWeekDisabled)) {
  310. this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
  311. }
  312. this.daysOfWeekDisabled = $.map(this.daysOfWeekDisabled, function (d) {
  313. return parseInt(d, 10);
  314. });
  315. this.update();
  316. this.updateNavArrows();
  317. },
  318. place: function(){
  319. if(this.isInline) return;
  320. var zIndex = parseInt(this.element.parents().filter(function() {
  321. return $(this).css('z-index') != 'auto';
  322. }).first().css('z-index'))+10;
  323. var offset, left;
  324. if (this.component) {
  325. offset = this.component.offset();
  326. left = offset.left;
  327. if (this.pickerPosition == 'bottom-left') {
  328. left += this.component.outerWidth() - this.picker.outerWidth();
  329. }
  330. } else {
  331. offset = this.element.offset();
  332. left = offset.left;
  333. }
  334. this.picker.css({
  335. top: offset.top + this.height,
  336. left: left,
  337. zIndex: zIndex
  338. });
  339. },
  340. update: function(){
  341. var date, fromArgs = false;
  342. if(arguments && arguments.length && (typeof arguments[0] === 'string' || arguments[0] instanceof Date)) {
  343. date = arguments[0];
  344. fromArgs = true;
  345. } else {
  346. date = this.isInput ? this.element.prop('value') : this.element.data('date') || this.element.find('input').prop('value') || new Date();
  347. }
  348. if (!date) {
  349. date = new Date();
  350. fromArgs = false;
  351. }
  352. this.date = DPGlobal.parseDate(date, this.format, this.language, this.formatType);
  353. if (fromArgs) this.setValue();
  354. if (this.date < this.startDate) {
  355. this.viewDate = new Date(this.startDate);
  356. } else if (this.date > this.endDate) {
  357. this.viewDate = new Date(this.endDate);
  358. } else {
  359. this.viewDate = new Date(this.date);
  360. }
  361. this.fill();
  362. },
  363. fillDow: function(){
  364. var dowCnt = this.weekStart,
  365. html = '<tr>';
  366. while (dowCnt < this.weekStart + 7) {
  367. html += '<th class="dow">'+dates[this.language].daysMin[(dowCnt++)%7]+'</th>';
  368. }
  369. html += '</tr>';
  370. this.picker.find('.datetimepicker-days thead').append(html);
  371. },
  372. fillMonths: function(){
  373. var html = '',
  374. i = 0;
  375. while (i < 12) {
  376. html += '<span class="month">'+dates[this.language].monthsShort[i++]+'</span>';
  377. }
  378. this.picker.find('.datetimepicker-months td').html(html);
  379. },
  380. fill: function() {
  381. if (this.date == null || this.viewDate == null) {
  382. return;
  383. }
  384. var d = new Date(this.viewDate),
  385. year = d.getUTCFullYear(),
  386. month = d.getUTCMonth(),
  387. dayMonth = d.getUTCDate(),
  388. hours = d.getUTCHours(),
  389. minutes = d.getUTCMinutes(),
  390. startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
  391. startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
  392. endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
  393. endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
  394. currentDate = (new UTCDate(this.date.getUTCFullYear(), this.date.getUTCMonth(), this.date.getUTCDate())).valueOf(),
  395. today = new Date();
  396. this.picker.find('.datetimepicker-days thead th:eq(1)')
  397. .text(dates[this.language].months[month]+' '+year);
  398. this.picker.find('.datetimepicker-hours thead th:eq(1)')
  399. .text(dayMonth+' '+dates[this.language].months[month]+' '+year);
  400. this.picker.find('.datetimepicker-minutes thead th:eq(1)')
  401. .text(dayMonth+' '+dates[this.language].months[month]+' '+year);
  402. this.picker.find('tfoot th.today')
  403. .text(dates[this.language].today)
  404. .toggle(this.todayBtn !== false);
  405. this.updateNavArrows();
  406. this.fillMonths();
  407. /*var prevMonth = UTCDate(year, month, 0,0,0,0,0);
  408. prevMonth.setUTCDate(prevMonth.getDate() - (prevMonth.getUTCDay() - this.weekStart + 7)%7);*/
  409. var prevMonth = UTCDate(year, month-1, 28,0,0,0,0),
  410. day = DPGlobal.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
  411. prevMonth.setUTCDate(day);
  412. prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
  413. var nextMonth = new Date(prevMonth);
  414. nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
  415. nextMonth = nextMonth.valueOf();
  416. var html = [];
  417. var clsName;
  418. while(prevMonth.valueOf() < nextMonth) {
  419. if (prevMonth.getUTCDay() == this.weekStart) {
  420. html.push('<tr>');
  421. }
  422. clsName = '';
  423. if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
  424. clsName += ' old';
  425. } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
  426. clsName += ' new';
  427. }
  428. // Compare internal UTC date with local today, not UTC today
  429. if (this.todayHighlight &&
  430. prevMonth.getUTCFullYear() == today.getFullYear() &&
  431. prevMonth.getUTCMonth() == today.getMonth() &&
  432. prevMonth.getUTCDate() == today.getDate()) {
  433. clsName += ' today';
  434. }
  435. if (prevMonth.valueOf() == currentDate) {
  436. clsName += ' active';
  437. }
  438. if ((prevMonth.valueOf() + 86400000) <= this.startDate || prevMonth.valueOf() > this.endDate ||
  439. $.inArray(prevMonth.getUTCDay(), this.daysOfWeekDisabled) !== -1) {
  440. clsName += ' disabled';
  441. }
  442. html.push('<td class="day'+clsName+'">'+prevMonth.getUTCDate() + '</td>');
  443. if (prevMonth.getUTCDay() == this.weekEnd) {
  444. html.push('</tr>');
  445. }
  446. prevMonth.setUTCDate(prevMonth.getUTCDate()+1);
  447. }
  448. this.picker.find('.datetimepicker-days tbody').empty().append(html.join(''));
  449. html = [];
  450. var txt = '', meridian = '', meridianOld = '';
  451. for (var i=0;i<24;i++) {
  452. var actual = UTCDate(year, month, dayMonth, i);
  453. clsName = '';
  454. // We want the previous hour for the startDate
  455. if ((actual.valueOf() + 3600000) <= this.startDate || actual.valueOf() > this.endDate) {
  456. clsName += ' disabled';
  457. } else if (hours == i) {
  458. clsName += ' active';
  459. }
  460. if (this.showMeridian && dates[this.language].meridiem.length == 2) {
  461. meridian = (i<12?dates[this.language].meridiem[0]:dates[this.language].meridiem[1]);
  462. if (meridian != meridianOld) {
  463. if (meridianOld != '') {
  464. html.push('</fieldset>');
  465. }
  466. html.push('<fieldset class="hour"><legend>'+meridian.toUpperCase()+'</legend>');
  467. }
  468. meridianOld = meridian;
  469. txt = (i%12?i%12:12);
  470. html.push('<span class="hour'+clsName+' hour_'+(i<12?'am':'pm')+'">'+txt+'</span>');
  471. if (i == 23) {
  472. html.push('</fieldset>');
  473. }
  474. } else {
  475. txt = i+':00';
  476. html.push('<span class="hour'+clsName+'">'+txt+'</span>');
  477. }
  478. }
  479. this.picker.find('.datetimepicker-hours td').html(html.join(''));
  480. html = [];
  481. txt = '', meridian = '', meridianOld = '';
  482. for(var i=0;i<60;i+=this.minuteStep) {
  483. var actual = UTCDate(year, month, dayMonth, hours, i);
  484. clsName = '';
  485. if (actual.valueOf() < this.startDate || actual.valueOf() > this.endDate) {
  486. clsName += ' disabled';
  487. } else if (Math.floor(minutes/this.minuteStep) == Math.floor(i/this.minuteStep)) {
  488. clsName += ' active';
  489. }
  490. if (this.showMeridian && dates[this.language].meridiem.length == 2) {
  491. meridian = (hours<12?dates[this.language].meridiem[0]:dates[this.language].meridiem[1]);
  492. if (meridian != meridianOld) {
  493. if (meridianOld != '') {
  494. html.push('</fieldset>');
  495. }
  496. html.push('<fieldset class="minute"><legend>'+meridian.toUpperCase()+'</legend>');
  497. }
  498. meridianOld = meridian;
  499. txt = (hours%12?hours%12:12);
  500. //html.push('<span class="minute'+clsName+' minute_'+(hours<12?'am':'pm')+'">'+txt+'</span>');
  501. html.push('<span class="minute'+clsName+'">'+txt+':'+(i<10?'0'+i:i)+'</span>');
  502. if (i == 59) {
  503. html.push('</fieldset>');
  504. }
  505. } else {
  506. txt = i+':00';
  507. //html.push('<span class="hour'+clsName+'">'+txt+'</span>');
  508. html.push('<span class="minute'+clsName+'">'+hours+':'+(i<10?'0'+i:i)+'</span>');
  509. }
  510. }
  511. this.picker.find('.datetimepicker-minutes td').html(html.join(''));
  512. var currentYear = this.date.getUTCFullYear();
  513. var months = this.picker.find('.datetimepicker-months')
  514. .find('th:eq(1)')
  515. .text(year)
  516. .end()
  517. .find('span').removeClass('active');
  518. if (currentYear == year) {
  519. months.eq(this.date.getUTCMonth()).addClass('active');
  520. }
  521. if (year < startYear || year > endYear) {
  522. months.addClass('disabled');
  523. }
  524. if (year == startYear) {
  525. months.slice(0, startMonth).addClass('disabled');
  526. }
  527. if (year == endYear) {
  528. months.slice(endMonth+1).addClass('disabled');
  529. }
  530. html = '';
  531. year = parseInt(year/10, 10) * 10;
  532. var yearCont = this.picker.find('.datetimepicker-years')
  533. .find('th:eq(1)')
  534. .text(year + '-' + (year + 9))
  535. .end()
  536. .find('td');
  537. year -= 1;
  538. for (var i = -1; i < 11; i++) {
  539. html += '<span class="year'+(i == -1 || i == 10 ? ' old' : '')+(currentYear == year ? ' active' : '')+(year < startYear || year > endYear ? ' disabled' : '')+'">'+year+'</span>';
  540. year += 1;
  541. }
  542. yearCont.html(html);
  543. },
  544. updateNavArrows: function() {
  545. var d = new Date(this.viewDate),
  546. year = d.getUTCFullYear(),
  547. month = d.getUTCMonth(),
  548. day = d.getUTCDate(),
  549. hour = d.getUTCHours();
  550. switch (this.viewMode) {
  551. case 0:
  552. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  553. && month <= this.startDate.getUTCMonth()
  554. && day <= this.startDate.getUTCDate()
  555. && hour <= this.startDate.getUTCHours()) {
  556. this.picker.find('.prev').css({visibility: 'hidden'});
  557. } else {
  558. this.picker.find('.prev').css({visibility: 'visible'});
  559. }
  560. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  561. && month >= this.endDate.getUTCMonth()
  562. && day >= this.endDate.getUTCDate()
  563. && hour >= this.endDate.getUTCHours()) {
  564. this.picker.find('.next').css({visibility: 'hidden'});
  565. } else {
  566. this.picker.find('.next').css({visibility: 'visible'});
  567. }
  568. break;
  569. case 1:
  570. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  571. && month <= this.startDate.getUTCMonth()
  572. && day <= this.startDate.getUTCDate()) {
  573. this.picker.find('.prev').css({visibility: 'hidden'});
  574. } else {
  575. this.picker.find('.prev').css({visibility: 'visible'});
  576. }
  577. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  578. && month >= this.endDate.getUTCMonth()
  579. && day >= this.endDate.getUTCDate()) {
  580. this.picker.find('.next').css({visibility: 'hidden'});
  581. } else {
  582. this.picker.find('.next').css({visibility: 'visible'});
  583. }
  584. break;
  585. case 2:
  586. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  587. && month <= this.startDate.getUTCMonth()) {
  588. this.picker.find('.prev').css({visibility: 'hidden'});
  589. } else {
  590. this.picker.find('.prev').css({visibility: 'visible'});
  591. }
  592. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  593. && month >= this.endDate.getUTCMonth()) {
  594. this.picker.find('.next').css({visibility: 'hidden'});
  595. } else {
  596. this.picker.find('.next').css({visibility: 'visible'});
  597. }
  598. break;
  599. case 3:
  600. case 4:
  601. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
  602. this.picker.find('.prev').css({visibility: 'hidden'});
  603. } else {
  604. this.picker.find('.prev').css({visibility: 'visible'});
  605. }
  606. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
  607. this.picker.find('.next').css({visibility: 'hidden'});
  608. } else {
  609. this.picker.find('.next').css({visibility: 'visible'});
  610. }
  611. break;
  612. }
  613. },
  614. click: function(e) {
  615. e.stopPropagation();
  616. e.preventDefault();
  617. var target = $(e.target).closest('span, td, th, legend');
  618. if (target.length == 1) {
  619. if (target.is('.disabled')) {
  620. this.element.trigger({
  621. type: 'outOfRange',
  622. date: this.viewDate,
  623. startDate: this.startDate,
  624. endDate: this.endDate
  625. });
  626. return;
  627. }
  628. switch(target[0].nodeName.toLowerCase()) {
  629. case 'th':
  630. switch(target[0].className) {
  631. case 'switch':
  632. this.showMode(1);
  633. break;
  634. case 'prev':
  635. case 'next':
  636. var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className == 'prev' ? -1 : 1);
  637. switch(this.viewMode){
  638. case 0:
  639. this.viewDate = this.moveHour(this.viewDate, dir);
  640. break;
  641. case 1:
  642. this.viewDate = this.moveDate(this.viewDate, dir);
  643. break;
  644. case 2:
  645. this.viewDate = this.moveMonth(this.viewDate, dir);
  646. break;
  647. case 3:
  648. case 4:
  649. this.viewDate = this.moveYear(this.viewDate, dir);
  650. break;
  651. }
  652. this.fill();
  653. break;
  654. case 'today':
  655. var date = new Date();
  656. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
  657. this.viewMode = this.startViewMode;
  658. this.showMode(0);
  659. this._setDate(date);
  660. break;
  661. }
  662. break;
  663. case 'span':
  664. if (!target.is('.disabled')) {
  665. var year = this.viewDate.getUTCFullYear(),
  666. month = this.viewDate.getUTCMonth(),
  667. day = this.viewDate.getUTCDate(),
  668. hours = this.viewDate.getUTCHours(),
  669. minutes = this.viewDate.getUTCMinutes(),
  670. seconds = this.viewDate.getUTCSeconds();
  671. if (target.is('.month')) {
  672. this.viewDate.setUTCDate(1);
  673. month = target.parent().find('span').index(target);
  674. this.viewDate.setUTCMonth(month);
  675. this.element.trigger({
  676. type: 'changeMonth',
  677. date: this.viewDate
  678. });
  679. if (this.viewSelect >= 3) {
  680. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  681. }
  682. } else if (target.is('.year')) {
  683. this.viewDate.setUTCDate(1);
  684. year = parseInt(target.text(), 10) || 0;
  685. this.viewDate.setUTCFullYear(year);
  686. this.element.trigger({
  687. type: 'changeYear',
  688. date: this.viewDate
  689. });
  690. if (this.viewSelect >= 4) {
  691. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  692. }
  693. } else if (target.is('.hour')){
  694. hours = parseInt(target.text(), 10) || 0;
  695. if (target.hasClass('hour_am') || target.hasClass('hour_pm')) {
  696. if (hours == 12 && target.hasClass('hour_am')) {
  697. hours = 0;
  698. } else if (hours != 12 && target.hasClass('hour_pm')) {
  699. hours += 12;
  700. }
  701. }
  702. this.viewDate.setUTCHours(hours);
  703. this.element.trigger({
  704. type: 'changeHour',
  705. date: this.viewDate
  706. });
  707. if (this.viewSelect >= 1) {
  708. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  709. }
  710. } else if (target.is('.minute')){
  711. minutes = parseInt(target.text().substr(target.text().indexOf(':')+1), 10) || 0;
  712. this.viewDate.setUTCMinutes(minutes);
  713. this.element.trigger({
  714. type: 'changeMinute',
  715. date: this.viewDate
  716. });
  717. if (this.viewSelect >= 0) {
  718. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  719. }
  720. }
  721. if (this.viewMode != 0) {
  722. var oldViewMode = this.viewMode;
  723. this.showMode(-1);
  724. this.fill();
  725. if (oldViewMode == this.viewMode && this.autoclose) {
  726. this.hide();
  727. }
  728. } else {
  729. this.fill();
  730. if (this.autoclose) {
  731. this.hide();
  732. }
  733. }
  734. }
  735. break;
  736. case 'td':
  737. if (target.is('.day') && !target.is('.disabled')){
  738. var day = parseInt(target.text(), 10) || 1;
  739. var year = this.viewDate.getUTCFullYear(),
  740. month = this.viewDate.getUTCMonth(),
  741. hours = this.viewDate.getUTCHours(),
  742. minutes = this.viewDate.getUTCMinutes(),
  743. seconds = this.viewDate.getUTCSeconds();
  744. if (target.is('.old')) {
  745. if (month === 0) {
  746. month = 11;
  747. year -= 1;
  748. } else {
  749. month -= 1;
  750. }
  751. } else if (target.is('.new')) {
  752. if (month == 11) {
  753. month = 0;
  754. year += 1;
  755. } else {
  756. month += 1;
  757. }
  758. }
  759. this.viewDate.setUTCDate(day);
  760. this.viewDate.setUTCMonth(month);
  761. this.viewDate.setUTCFullYear(year);
  762. this.element.trigger({
  763. type: 'changeDay',
  764. date: this.viewDate
  765. });
  766. if (this.viewSelect >= 2) {
  767. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  768. }
  769. }
  770. var oldViewMode = this.viewMode;
  771. this.showMode(-1);
  772. this.fill();
  773. if (oldViewMode == this.viewMode && this.autoclose) {
  774. this.hide();
  775. }
  776. break;
  777. }
  778. }
  779. },
  780. _setDate: function(date, which){
  781. if (!which || which == 'date')
  782. this.date = date;
  783. if (!which || which == 'view')
  784. this.viewDate = date;
  785. this.fill();
  786. this.element.trigger({
  787. type: 'changeDate',
  788. date: this.date
  789. });
  790. this.setValue();
  791. var element;
  792. if (this.isInput) {
  793. element = this.element;
  794. } else if (this.component){
  795. element = this.element.find('input');
  796. }
  797. if (element) {
  798. element.change();
  799. if (this.autoclose && (!which || which == 'date')) {
  800. //this.hide();
  801. }
  802. }
  803. },
  804. moveHour: function(date, dir){
  805. if (!dir) return date;
  806. var new_date = new Date(date.valueOf());
  807. dir = dir > 0 ? 1 : -1;
  808. new_date.setUTCHours(new_date.getUTCHours() + dir);
  809. return new_date;
  810. },
  811. moveDate: function(date, dir){
  812. if (!dir) return date;
  813. var new_date = new Date(date.valueOf());
  814. dir = dir > 0 ? 1 : -1;
  815. new_date.setUTCDate(new_date.getUTCDate() + dir);
  816. return new_date;
  817. },
  818. moveMonth: function(date, dir){
  819. if (!dir) return date;
  820. var new_date = new Date(date.valueOf()),
  821. day = new_date.getUTCDate(),
  822. month = new_date.getUTCMonth(),
  823. mag = Math.abs(dir),
  824. new_month, test;
  825. dir = dir > 0 ? 1 : -1;
  826. if (mag == 1){
  827. test = dir == -1
  828. // If going back one month, make sure month is not current month
  829. // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
  830. ? function(){ return new_date.getUTCMonth() == month; }
  831. // If going forward one month, make sure month is as expected
  832. // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
  833. : function(){ return new_date.getUTCMonth() != new_month; };
  834. new_month = month + dir;
  835. new_date.setUTCMonth(new_month);
  836. // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
  837. if (new_month < 0 || new_month > 11)
  838. new_month = (new_month + 12) % 12;
  839. } else {
  840. // For magnitudes >1, move one month at a time...
  841. for (var i=0; i<mag; i++)
  842. // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
  843. new_date = this.moveMonth(new_date, dir);
  844. // ...then reset the day, keeping it in the new month
  845. new_month = new_date.getUTCMonth();
  846. new_date.setUTCDate(day);
  847. test = function(){ return new_month != new_date.getUTCMonth(); };
  848. }
  849. // Common date-resetting loop -- if date is beyond end of month, make it
  850. // end of month
  851. while (test()){
  852. new_date.setUTCDate(--day);
  853. new_date.setUTCMonth(new_month);
  854. }
  855. return new_date;
  856. },
  857. moveYear: function(date, dir){
  858. return this.moveMonth(date, dir*12);
  859. },
  860. dateWithinRange: function(date){
  861. return date >= this.startDate && date <= this.endDate;
  862. },
  863. keydown: function(e){
  864. if (this.picker.is(':not(:visible)')){
  865. if (e.keyCode == 27) // allow escape to hide and re-show picker
  866. this.show();
  867. return;
  868. }
  869. var dateChanged = false,
  870. dir, day, month,
  871. newDate, newViewDate;
  872. switch(e.keyCode){
  873. case 27: // escape
  874. this.hide();
  875. e.preventDefault();
  876. break;
  877. case 37: // left
  878. case 39: // right
  879. if (!this.keyboardNavigation) break;
  880. dir = e.keyCode == 37 ? -1 : 1;
  881. if (e.ctrlKey){
  882. newDate = this.moveYear(this.date, dir);
  883. newViewDate = this.moveYear(this.viewDate, dir);
  884. } else if (e.shiftKey){
  885. newDate = this.moveMonth(this.date, dir);
  886. newViewDate = this.moveMonth(this.viewDate, dir);
  887. } else {
  888. newDate = new Date(this.date);
  889. newDate.setUTCDate(this.date.getUTCDate() + dir);
  890. newViewDate = new Date(this.viewDate);
  891. newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
  892. }
  893. if (this.dateWithinRange(newDate)){
  894. this.date = newDate;
  895. this.viewDate = newViewDate;
  896. this.setValue();
  897. this.update();
  898. e.preventDefault();
  899. dateChanged = true;
  900. }
  901. break;
  902. case 38: // up
  903. case 40: // down
  904. if (!this.keyboardNavigation) break;
  905. dir = e.keyCode == 38 ? -1 : 1;
  906. if (e.ctrlKey){
  907. newDate = this.moveYear(this.date, dir);
  908. newViewDate = this.moveYear(this.viewDate, dir);
  909. } else if (e.shiftKey){
  910. newDate = this.moveMonth(this.date, dir);
  911. newViewDate = this.moveMonth(this.viewDate, dir);
  912. } else {
  913. newDate = new Date(this.date);
  914. newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
  915. newViewDate = new Date(this.viewDate);
  916. newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
  917. }
  918. if (this.dateWithinRange(newDate)){
  919. this.date = newDate;
  920. this.viewDate = newViewDate;
  921. this.setValue();
  922. this.update();
  923. e.preventDefault();
  924. dateChanged = true;
  925. }
  926. break;
  927. case 13: // enter
  928. this.hide();
  929. e.preventDefault();
  930. break;
  931. case 9: // tab
  932. this.hide();
  933. break;
  934. }
  935. if (dateChanged){
  936. this.element.trigger({
  937. type: 'changeDate',
  938. date: this.date
  939. });
  940. var element;
  941. if (this.isInput) {
  942. element = this.element;
  943. } else if (this.component){
  944. element = this.element.find('input');
  945. }
  946. if (element) {
  947. element.change();
  948. }
  949. }
  950. },
  951. showMode: function(dir) {
  952. if (dir) {
  953. var newViewMode = Math.max(0, Math.min(DPGlobal.modes.length - 1, this.viewMode + dir));
  954. if (newViewMode >= this.minView && newViewMode <= this.maxView) {
  955. this.viewMode = newViewMode;
  956. }
  957. }
  958. /*
  959. vitalets: fixing bug of very special conditions:
  960. jquery 1.7.1 + webkit + show inline datetimepicker in bootstrap popover.
  961. Method show() does not set display css correctly and datetimepicker is not shown.
  962. Changed to .css('display', 'block') solve the problem.
  963. See https://github.com/vitalets/x-editable/issues/37
  964. In jquery 1.7.2+ everything works fine.
  965. */
  966. //this.picker.find('>div').hide().filter('.datetimepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
  967. this.picker.find('>div').hide().filter('.datetimepicker-'+DPGlobal.modes[this.viewMode].clsName).css('display', 'block');
  968. this.updateNavArrows();
  969. },
  970. reset: function(e) {
  971. this._setDate(null, 'date');
  972. }
  973. };
  974. $.fn.datetimepicker = function ( option ) {
  975. var args = Array.apply(null, arguments);
  976. args.shift();
  977. return this.each(function () {
  978. var $this = $(this),
  979. data = $this.data('datetimepicker'),
  980. options = typeof option == 'object' && option;
  981. if (!data) {
  982. $this.data('datetimepicker', (data = new Datetimepicker(this, $.extend({}, $.fn.datetimepicker.defaults,options))));
  983. }
  984. if (typeof option == 'string' && typeof data[option] == 'function') {
  985. data[option].apply(data, args);
  986. }
  987. });
  988. };
  989. $.fn.datetimepicker.defaults = {
  990. };
  991. $.fn.datetimepicker.Constructor = Datetimepicker;
  992. var dates = $.fn.datetimepicker.dates = {
  993. en: {
  994. days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
  995. daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  996. daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
  997. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  998. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  999. meridiem: ["am", "pm"],
  1000. suffix: ["st", "nd", "rd", "th"],
  1001. today: "Today"
  1002. }
  1003. };
  1004. var DPGlobal = {
  1005. modes: [
  1006. {
  1007. clsName: 'minutes',
  1008. navFnc: 'Hours',
  1009. navStep: 1
  1010. },
  1011. {
  1012. clsName: 'hours',
  1013. navFnc: 'Date',
  1014. navStep: 1
  1015. },
  1016. {
  1017. clsName: 'days',
  1018. navFnc: 'Month',
  1019. navStep: 1
  1020. },
  1021. {
  1022. clsName: 'months',
  1023. navFnc: 'FullYear',
  1024. navStep: 1
  1025. },
  1026. {
  1027. clsName: 'years',
  1028. navFnc: 'FullYear',
  1029. navStep: 10
  1030. }],
  1031. isLeapYear: function (year) {
  1032. return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
  1033. },
  1034. getDaysInMonth: function (year, month) {
  1035. return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
  1036. },
  1037. getDefaultFormat: function (type, field) {
  1038. if (type == "standard") {
  1039. if (field == 'input')
  1040. return 'yyyy-mm-dd hh:ii';
  1041. else
  1042. return 'yyyy-mm-dd hh:ii:ss';
  1043. } else if (type == "php") {
  1044. if (field == 'input')
  1045. return 'Y-m-d H:i';
  1046. else
  1047. return 'Y-m-d H:i:s';
  1048. } else {
  1049. throw new Error("Invalid format type.");
  1050. }
  1051. },
  1052. validParts: function (type) {
  1053. if (type == "standard") {
  1054. return /hh?|HH?|p|P|ii?|ss?|dd?|mm?|MM?|yy(?:yy)?/g;
  1055. } else if (type == "php") {
  1056. return /[dDjlNwzFmMnStyYaABgGhHis]/g;
  1057. } else {
  1058. throw new Error("Invalid format type.");
  1059. }
  1060. },
  1061. nonpunctuation: /[^ -\/:-@\[-`{-~\t\n\rTZ]+/g,
  1062. parseFormat: function(format, type){
  1063. // IE treats \0 as a string end in inputs (truncating the value),
  1064. // so it's a bad format delimiter, anyway
  1065. var separators = format.replace(this.validParts(type), '\0').split('\0'),
  1066. parts = format.match(this.validParts(type));
  1067. if (!separators || !separators.length || !parts || parts.length == 0){
  1068. throw new Error("Invalid date format.");
  1069. }
  1070. return {separators: separators, parts: parts};
  1071. },
  1072. parseDate: function(date, format, language, type) {
  1073. if (date instanceof Date) {
  1074. var dateUTC = new Date(date.valueOf() - date.getTimezoneOffset() * 60000);
  1075. return dateUTC;
  1076. }
  1077. if (/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(date)) {
  1078. format = this.parseFormat('yyyy-mm-dd', type);
  1079. }
  1080. if (/^\d{4}\-\d{1,2}\-\d{1,2}[T ]\d{1,2}\:\d{1,2}$/.test(date)) {
  1081. format = this.parseFormat('yyyy-mm-dd hh:ii', type);
  1082. }
  1083. if (/^\d{4}\-\d{1,2}\-\d{1,2}[T ]\d{1,2}\:\d{1,2}\:\d{1,2}[Z]{0,1}$/.test(date)) {
  1084. format = this.parseFormat('yyyy-mm-dd hh:ii:ss', type);
  1085. }
  1086. if (/^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$/.test(date)) {
  1087. var part_re = /([-+]\d+)([dmwy])/,
  1088. parts = date.match(/([-+]\d+)([dmwy])/g),
  1089. part, dir;
  1090. date = new Date();
  1091. for (var i=0; i<parts.length; i++) {
  1092. part = part_re.exec(parts[i]);
  1093. dir = parseInt(part[1]);
  1094. switch(part[2]){
  1095. case 'd':
  1096. date.setUTCDate(date.getUTCDate() + dir);
  1097. break;
  1098. case 'm':
  1099. date = Datetimepicker.prototype.moveMonth.call(Datetimepicker.prototype, date, dir);
  1100. break;
  1101. case 'w':
  1102. date.setUTCDate(date.getUTCDate() + dir * 7);
  1103. break;
  1104. case 'y':
  1105. date = Datetimepicker.prototype.moveYear.call(Datetimepicker.prototype, date, dir);
  1106. break;
  1107. }
  1108. }
  1109. return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
  1110. }
  1111. var parts = date && date.match(this.nonpunctuation) || [],
  1112. date = new Date(0, 0, 0, 0, 0, 0),
  1113. parsed = {},
  1114. setters_order = ['hh', 'h', 'ii', 'i', 'ss', 's', 'yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd', 'H', 'HH', 'p', 'P'],
  1115. setters_map = {
  1116. hh: function(d,v){ return d.setUTCHours(v); },
  1117. h: function(d,v){ return d.setUTCHours(v); },
  1118. HH: function(d,v){ return d.setUTCHours(v==12?0:v); },
  1119. H: function(d,v){ return d.setUTCHours(v==12?0:v); },
  1120. ii: function(d,v){ return d.setUTCMinutes(v); },
  1121. i: function(d,v){ return d.setUTCMinutes(v); },
  1122. ss: function(d,v){ return d.setUTCSeconds(v); },
  1123. s: function(d,v){ return d.setUTCSeconds(v); },
  1124. yyyy: function(d,v){ return d.setUTCFullYear(v); },
  1125. yy: function(d,v){ return d.setUTCFullYear(2000+v); },
  1126. m: function(d,v){
  1127. v -= 1;
  1128. while (v<0) v += 12;
  1129. v %= 12;
  1130. d.setUTCMonth(v);
  1131. while (d.getUTCMonth() != v)
  1132. d.setUTCDate(d.getUTCDate()-1);
  1133. return d;
  1134. },
  1135. d: function(d,v){ return d.setUTCDate(v); }
  1136. },
  1137. val, filtered, part;
  1138. setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
  1139. setters_map['dd'] = setters_map['d'];
  1140. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
  1141. if (parts.length == format.parts.length) {
  1142. for (var i=0, cnt = format.parts.length; i < cnt; i++) {
  1143. val = parseInt(parts[i], 10);
  1144. part = format.parts[i];
  1145. if (isNaN(val)) {
  1146. switch(part) {
  1147. case 'MM':
  1148. filtered = $(dates[language].months).filter(function(){
  1149. var m = this.slice(0, parts[i].length),
  1150. p = parts[i].slice(0, m.length);
  1151. return m == p;
  1152. });
  1153. val = $.inArray(filtered[0], dates[language].months) + 1;
  1154. break;
  1155. case 'M':
  1156. filtered = $(dates[language].monthsShort).filter(function(){
  1157. var m = this.slice(0, parts[i].length),
  1158. p = parts[i].slice(0, m.length);
  1159. return m == p;
  1160. });
  1161. val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
  1162. break;
  1163. }
  1164. }
  1165. parsed[part] = val;
  1166. }
  1167. for (var i=0, s; i<setters_order.length; i++){
  1168. s = setters_order[i];
  1169. if (s in parsed && !isNaN(parsed[s]))
  1170. setters_map[s](date, parsed[s])
  1171. }
  1172. }
  1173. return date;
  1174. },
  1175. formatDate: function(date, format, language, type){
  1176. if (date == null) {
  1177. return '';
  1178. }
  1179. var val;
  1180. if (type == 'standard') {
  1181. val = {
  1182. // year
  1183. yy: date.getUTCFullYear().toString().substring(2),
  1184. yyyy: date.getUTCFullYear(),
  1185. // month
  1186. m: date.getUTCMonth() + 1,
  1187. M: dates[language].monthsShort[date.getUTCMonth()],
  1188. MM: dates[language].months[date.getUTCMonth()],
  1189. // day
  1190. d: date.getUTCDate(),
  1191. P: (dates[language].meridiem.length==2?dates[language].meridiem[date.getUTCHours()<12?0:1]:''),
  1192. // hour
  1193. h: date.getUTCHours(),
  1194. // minute
  1195. i: date.getUTCMinutes(),
  1196. // second
  1197. s: date.getUTCSeconds(),
  1198. };
  1199. val.H = (val.h%12==0? 12 : val.h%12);
  1200. val.HH = (val.H < 10 ? '0' : '') + val.H;
  1201. val.p = val.P.toUpperCase();
  1202. val.hh = (val.h < 10 ? '0' : '') + val.h;
  1203. val.ii = (val.i < 10 ? '0' : '') + val.i;
  1204. val.ss = (val.s < 10 ? '0' : '') + val.s;
  1205. val.dd = (val.d < 10 ? '0' : '') + val.d;
  1206. val.mm = (val.m < 10 ? '0' : '') + val.m;
  1207. } else if (type == 'php') {
  1208. // php format
  1209. val = {
  1210. // year
  1211. y: date.getUTCFullYear().toString().substring(2),
  1212. Y: date.getUTCFullYear(),
  1213. // month
  1214. F: dates[language].months[date.getUTCMonth()],
  1215. M: dates[language].monthsShort[date.getUTCMonth()],
  1216. n: date.getUTCMonth() + 1,
  1217. t: DPGlobal.getDaysInMonth(date.getUTCFullYear(), date.getUTCMonth()),
  1218. // day
  1219. j: date.getUTCDate(),
  1220. l: dates[language].days[date.getUTCDay()],
  1221. D: dates[language].daysShort[date.getUTCDay()],
  1222. w: date.getUTCDay(), // 0 -> 6
  1223. N: (date.getUTCDay()==0?7:date.getUTCDay()), // 1 -> 7
  1224. S: (date.getUTCDate()%10<=dates[language].suffix.length?dates[language].suffix[date.getUTCDate()%10-1]:''),
  1225. // hour
  1226. a: (dates[language].meridiem.length==2?dates[language].meridiem[date.getUTCHours()<12?0:1]:''),
  1227. g: (date.getUTCHours()%12==0?12:date.getUTCHours()%12),
  1228. G: date.getUTCHours(),
  1229. // minute
  1230. i: date.getUTCMinutes(),
  1231. // second
  1232. s: date.getUTCSeconds()
  1233. };
  1234. val.m = (val.n < 10 ? '0' : '') + val.n;
  1235. val.d = (val.j < 10 ? '0' : '') + val.j;
  1236. val.A = val.a.toString().toUpperCase();
  1237. val.h = (val.g < 10 ? '0' : '') + val.g;
  1238. val.H = (val.G < 10 ? '0' : '') + val.G;
  1239. val.i = (val.i < 10 ? '0' : '') + val.i;
  1240. val.s = (val.s < 10 ? '0' : '') + val.s;
  1241. } else {
  1242. throw new Error("Invalid format type.");
  1243. }
  1244. var date = [],
  1245. seps = $.extend([], format.separators);
  1246. for (var i=0, cnt = format.parts.length; i < cnt; i++) {
  1247. if (seps.length)
  1248. date.push(seps.shift())
  1249. date.push(val[format.parts[i]]);
  1250. }
  1251. return date.join('');
  1252. },
  1253. convertViewMode: function(viewMode){
  1254. switch (viewMode) {
  1255. case 4:
  1256. case 'decade':
  1257. viewMode = 4;
  1258. break;
  1259. case 3:
  1260. case 'year':
  1261. viewMode = 3;
  1262. break;
  1263. case 2:
  1264. case 'month':
  1265. viewMode = 2;
  1266. break;
  1267. case 1:
  1268. case 'day':
  1269. viewMode = 1;
  1270. break;
  1271. case 0:
  1272. case 'hour':
  1273. viewMode = 0;
  1274. break;
  1275. }
  1276. return viewMode;
  1277. },
  1278. headTemplate: '<thead>'+
  1279. '<tr>'+
  1280. '<th class="prev"><i class="icon-arrow-left"/></th>'+
  1281. '<th colspan="5" class="switch"></th>'+
  1282. '<th class="next"><i class="icon-arrow-right"/></th>'+
  1283. '</tr>'+
  1284. '</thead>',
  1285. contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>',
  1286. footTemplate: '<tfoot><tr><th colspan="7" class="today"></th></tr></tfoot>'
  1287. };
  1288. DPGlobal.template = '<div class="datetimepicker">'+
  1289. '<div class="datetimepicker-minutes">'+
  1290. '<table class=" table-condensed">'+
  1291. DPGlobal.headTemplate+
  1292. DPGlobal.contTemplate+
  1293. DPGlobal.footTemplate+
  1294. '</table>'+
  1295. '</div>'+
  1296. '<div class="datetimepicker-hours">'+
  1297. '<table class=" table-condensed">'+
  1298. DPGlobal.headTemplate+
  1299. DPGlobal.contTemplate+
  1300. DPGlobal.footTemplate+
  1301. '</table>'+
  1302. '</div>'+
  1303. '<div class="datetimepicker-days">'+
  1304. '<table class=" table-condensed">'+
  1305. DPGlobal.headTemplate+
  1306. '<tbody></tbody>'+
  1307. DPGlobal.footTemplate+
  1308. '</table>'+
  1309. '</div>'+
  1310. '<div class="datetimepicker-months">'+
  1311. '<table class="table-condensed">'+
  1312. DPGlobal.headTemplate+
  1313. DPGlobal.contTemplate+
  1314. DPGlobal.footTemplate+
  1315. '</table>'+
  1316. '</div>'+
  1317. '<div class="datetimepicker-years">'+
  1318. '<table class="table-condensed">'+
  1319. DPGlobal.headTemplate+
  1320. DPGlobal.contTemplate+
  1321. DPGlobal.footTemplate+
  1322. '</table>'+
  1323. '</div>'+
  1324. '</div>';
  1325. $.fn.datetimepicker.DPGlobal = DPGlobal;
  1326. }( window.jQuery );