No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

537 líneas
23 KiB

  1. /*
  2. * jQuery Format Plugin
  3. * http://www.asual.com/jquery/format/
  4. *
  5. * Copyright (c) 2009-2011 Rostislav Hristov
  6. * Uses code by Matt Kruse
  7. * Dual licensed under the MIT or GPL Version 2 licenses.
  8. * http://jquery.org/license
  9. */
  10. ;(function (factory) {
  11. "use strict";
  12. if (typeof define === 'function' && define.amd) {
  13. // AMD. Register as an anonymous module.
  14. define(['jquery'], factory);
  15. } else if (typeof exports === 'object' && typeof require === 'function') {
  16. // Node/CommonJS
  17. factory(require('jquery'));
  18. } else {
  19. // Browser globals
  20. factory(jQuery);
  21. }
  22. }(function ($) {
  23. $.format = (function () {
  24. var UNDEFINED = 'undefined',
  25. TRUE = true,
  26. FALSE = false,
  27. _locale = {
  28. date: {
  29. format: 'MMM dd, yyyy h:mm:ss a',
  30. monthsFull: ['January','February','March','April','May','June',
  31. 'July','August','September','October','November','December'],
  32. monthsShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
  33. daysFull: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
  34. daysShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
  35. shortDateFormat: 'M/d/yyyy h:mm a',
  36. longDateFormat: 'EEEE, MMMM dd, yyyy h:mm:ss a'
  37. },
  38. number: {
  39. format: '#,##0.0#',
  40. groupingSeparator: ',',
  41. decimalSeparator: '.'
  42. }
  43. };
  44. return {
  45. locale: function(value) {
  46. a = {a: 6};
  47. if (value) {
  48. for (var p in value) {
  49. for (var v in value[p]) {
  50. _locale[p][v] = value[p][v];
  51. }
  52. }
  53. }
  54. return _locale;
  55. },
  56. date: function(value, format) {
  57. var i = 0,
  58. j = 0,
  59. l = 0,
  60. c = '',
  61. token = '',
  62. x,
  63. y;
  64. if (typeof value == 'string') {
  65. var getNumber = function (str, p, minlength, maxlength) {
  66. for (var x = maxlength; x >= minlength; x--) {
  67. var token = str.substring(p, p + x);
  68. if (token.length >= minlength && /^\d+$/.test(token)) {
  69. return token;
  70. }
  71. }
  72. return null;
  73. };
  74. if (typeof format == UNDEFINED) {
  75. format = _locale.date.format;
  76. }
  77. var _strict = false,
  78. pos = 0,
  79. now = new Date(0, 0, 0, 0, 0, 0, 0),
  80. year = now.getYear(),
  81. month = now.getMonth() + 1,
  82. date = 1,
  83. hh = now.getHours(),
  84. mm = now.getMinutes(),
  85. ss = now.getSeconds(),
  86. SSS = now.getMilliseconds(),
  87. ampm = '',
  88. monthName,
  89. dayName;
  90. while (i < format.length) {
  91. token = '';
  92. c = format.charAt(i);
  93. while ((format.charAt(i) == c) && (i < format.length)) {
  94. token += format.charAt(i++);
  95. }
  96. if (token.indexOf('MMMM') > - 1 && token.length > 4) {
  97. token = 'MMMM';
  98. }
  99. if (token.indexOf('EEEE') > - 1 && token.length > 4) {
  100. token = 'EEEE';
  101. }
  102. if (token == 'yyyy' || token == 'yy' || token == 'y') {
  103. if (token == 'yyyy') {
  104. x = 4;
  105. y = 4;
  106. }
  107. if (token == 'yy') {
  108. x = 2;
  109. y = 2;
  110. }
  111. if (token == 'y') {
  112. x = 2;
  113. y = 4;
  114. }
  115. year = getNumber(value, pos, x, y);
  116. if (year === null) {
  117. return 0;
  118. }
  119. pos += year.length;
  120. if (year.length == 2) {
  121. year = parseInt(year, 10);
  122. if (year > 70) {
  123. year = 1900 + year;
  124. } else {
  125. year = 2000 + year;
  126. }
  127. }
  128. } else if (token == 'MMMM'){
  129. month = 0;
  130. for (j = 0, l = _locale.date.monthsFull.length; j < l; j++) {
  131. monthName = _locale.date.monthsFull[j];
  132. if (value.substring(pos, pos + monthName.length).toLowerCase() == monthName.toLowerCase()) {
  133. month = j + 1;
  134. pos += monthName.length;
  135. break;
  136. }
  137. }
  138. if ((month < 1) || (month > 12)){
  139. return 0;
  140. }
  141. } else if (token == 'MMM'){
  142. month = 0;
  143. for (j = 0, l = _locale.date.monthsShort.length; j < l; j++) {
  144. monthName = _locale.date.monthsShort[j];
  145. if (value.substring(pos, pos + monthName.length).toLowerCase() == monthName.toLowerCase()) {
  146. month = j + 1;
  147. pos += monthName.length;
  148. break;
  149. }
  150. }
  151. if ((month < 1) || (month > 12)){
  152. return 0;
  153. }
  154. } else if (token == 'EEEE'){
  155. for (j = 0, l = _locale.date.daysFull.length; j < l; j++) {
  156. dayName = _locale.date.daysFull[j];
  157. if (value.substring(pos, pos + dayName.length).toLowerCase() == dayName.toLowerCase()) {
  158. pos += dayName.length;
  159. break;
  160. }
  161. }
  162. } else if (token == 'EEE'){
  163. for (j = 0, l =_locale.date.daysShort.length; j < l; j++) {
  164. dayName = _locale.date.daysShort[j];
  165. if (value.substring(pos, pos + dayName.length).toLowerCase() == dayName.toLowerCase()) {
  166. pos += dayName.length;
  167. break;
  168. }
  169. }
  170. } else if (token == 'MM' || token == 'M') {
  171. month = getNumber(value, pos, _strict ? token.length : 1, 2);
  172. if (month === null || (month < 1) || (month > 12)){
  173. return 0;
  174. }
  175. pos += month.length;
  176. } else if (token == 'dd' || token == 'd') {
  177. date = getNumber(value, pos, _strict ? token.length : 1, 2);
  178. if (date === null || (date < 1) || (date > 31)){
  179. return 0;
  180. }
  181. pos += date.length;
  182. } else if (token == 'hh' || token == 'h') {
  183. hh = getNumber(value, pos, _strict ? token.length : 1, 2);
  184. if (hh === null || (hh < 1) || (hh > 12)) {
  185. return 0;
  186. }
  187. pos += hh.length;
  188. } else if (token == 'HH' || token == 'H') {
  189. hh = getNumber(value, pos, _strict ? token.length : 1, 2);
  190. if(hh === null || (hh < 0) || (hh > 23)){
  191. return 0;
  192. }
  193. pos += hh.length;
  194. } else if (token == 'KK' || token == 'K') {
  195. hh = getNumber(value, pos, _strict ? token.length : 1, 2);
  196. if (hh === null || (hh < 0) || (hh > 11)){
  197. return 0;
  198. }
  199. pos += hh.length;
  200. } else if (token == 'kk' || token == 'k') {
  201. hh = getNumber(value, pos, _strict ? token.length : 1, 2);
  202. if (hh === null || (hh < 1) || (hh > 24)){
  203. return 0;
  204. }
  205. pos += hh.length;
  206. hh--;
  207. } else if (token == 'mm' || token == 'm') {
  208. mm = getNumber(value, pos, _strict ? token.length : 1, 2);
  209. if (mm === null || (mm < 0) || ( mm > 59)) {
  210. return 0;
  211. }
  212. pos += mm.length;
  213. } else if (token == 'ss' || token == 's') {
  214. ss = getNumber(value, pos, _strict ? token.length : 1, 2);
  215. if (ss === null || (ss < 0) || (ss > 59)){
  216. return 0;
  217. }
  218. pos += ss.length;
  219. } else if (token == 'SSS' || token == 'SS' || token == 'S') {
  220. SSS = getNumber(value, pos, _strict ? token.length : 1, 3);
  221. if (SSS === null || (SSS < 0) || (SSS > 999)){
  222. return 0;
  223. }
  224. pos += SSS.length;
  225. } else if (token == 'a') {
  226. var ap = value.substring(pos, pos + 2).toLowerCase();
  227. if (ap == 'am') {
  228. ampm = 'AM';
  229. } else if (ap == 'pm') {
  230. ampm = 'PM';
  231. } else {
  232. return 0;
  233. }
  234. pos += 2;
  235. } else {
  236. if (token != value.substring(pos, pos + token.length)) {
  237. return 0;
  238. } else {
  239. pos += token.length;
  240. }
  241. }
  242. }
  243. if (pos != value.length) {
  244. return 0;
  245. }
  246. if (month == 2) {
  247. if (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)) {
  248. if (date > 29) {
  249. return 0;
  250. }
  251. } else {
  252. if (date > 28) {
  253. return 0;
  254. }
  255. }
  256. }
  257. if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
  258. if (date > 30) {
  259. return 0;
  260. }
  261. }
  262. if (hh < 12 && ampm == 'PM') {
  263. hh = hh - 0 + 12;
  264. } else if (hh > 11 && ampm == 'AM') {
  265. hh -= 12;
  266. }
  267. return (new Date(year, month - 1, date, hh, mm, ss, SSS));
  268. } else {
  269. var formatNumber = function (n, s) {
  270. if (typeof s == UNDEFINED || s == 2) {
  271. return (n >= 0 && n < 10 ? '0' : '') + n;
  272. } else {
  273. if (n >= 0 && n < 10) {
  274. return '00' + n;
  275. }
  276. if (n >= 10 && n <100) {
  277. return '0' + n;
  278. }
  279. return n;
  280. }
  281. };
  282. if (typeof format == UNDEFINED) {
  283. format = _locale.date.format;
  284. }
  285. y = value.getYear();
  286. if (y < 1000) {
  287. y = String(y + 1900);
  288. }
  289. var M = value.getMonth() + 1,
  290. d = value.getDate(),
  291. E = value.getDay(),
  292. H = value.getHours(),
  293. m = value.getMinutes(),
  294. s = value.getSeconds(),
  295. S = value.getMilliseconds();
  296. value = {
  297. y: y,
  298. yyyy: y,
  299. yy: String(y).substring(2, 4),
  300. M: M,
  301. MM: formatNumber(M),
  302. MMM: _locale.date.monthsShort[M-1],
  303. MMMM: _locale.date.monthsFull[M-1],
  304. d: d,
  305. dd: formatNumber(d),
  306. EEE: _locale.date.daysShort[E],
  307. EEEE: _locale.date.daysFull[E],
  308. H: H,
  309. HH: formatNumber(H)
  310. };
  311. if (H === 0) {
  312. value.h = 12;
  313. } else if (H > 12) {
  314. value.h = H - 12;
  315. } else {
  316. value.h = H;
  317. }
  318. value.hh = formatNumber(value.h);
  319. value.k = H !== 0 ? H : 24;
  320. value.kk = formatNumber(value.k);
  321. if (H > 11) {
  322. value.K = H - 12;
  323. } else {
  324. value.K = H;
  325. }
  326. value.KK = formatNumber(value.K);
  327. if (H > 11) {
  328. value.a = 'PM';
  329. } else {
  330. value.a = 'AM';
  331. }
  332. value.m = m;
  333. value.mm = formatNumber(m);
  334. value.s = s;
  335. value.ss = formatNumber(s);
  336. value.S = S;
  337. value.SS = formatNumber(S);
  338. value.SSS = formatNumber(S, 3);
  339. var result = '';
  340. i = 0;
  341. c = '';
  342. token = '';
  343. s = false;
  344. while (i < format.length) {
  345. token = '';
  346. c = format.charAt(i);
  347. if (c == '\'') {
  348. i++;
  349. if (format.charAt(i) == c) {
  350. result = result + c;
  351. i++;
  352. } else {
  353. s = !s;
  354. }
  355. } else {
  356. while (format.charAt(i) == c) {
  357. token += format.charAt(i++);
  358. }
  359. if (token.indexOf('MMMM') != -1 && token.length > 4) {
  360. token = 'MMMM';
  361. }
  362. if (token.indexOf('EEEE') != -1 && token.length > 4) {
  363. token = 'EEEE';
  364. }
  365. if (typeof value[token] != UNDEFINED && !s) {
  366. result = result + value[token];
  367. } else {
  368. result = result + token;
  369. }
  370. }
  371. }
  372. return result;
  373. }
  374. },
  375. number: function(value, format) {
  376. var groupingSeparator,
  377. groupingIndex,
  378. decimalSeparator,
  379. decimalIndex,
  380. roundFactor,
  381. result,
  382. i;
  383. if (typeof value == 'string') {
  384. groupingSeparator = _locale.number.groupingSeparator;
  385. decimalSeparator = _locale.number.decimalSeparator;
  386. decimalIndex = value.indexOf(decimalSeparator);
  387. roundFactor = 1;
  388. if (decimalIndex != -1) {
  389. roundFactor = Math.pow(10, value.length - decimalIndex - 1);
  390. }
  391. value = value.replace(new RegExp('[' + groupingSeparator + ']', 'g'), '');
  392. value = value.replace(new RegExp('[' + decimalSeparator + ']'), '.');
  393. return Math.round(value*roundFactor)/roundFactor;
  394. } else {
  395. if (typeof format == UNDEFINED || format.length < 1) {
  396. format = _locale.number.format;
  397. }
  398. groupingSeparator = ',';
  399. groupingIndex = format.lastIndexOf(groupingSeparator);
  400. decimalSeparator = '.';
  401. decimalIndex = format.indexOf(decimalSeparator);
  402. var integer = '',
  403. fraction = '',
  404. negative = value < 0,
  405. minFraction = format.substr(decimalIndex + 1).replace(/#/g, '').length,
  406. maxFraction = format.substr(decimalIndex + 1).length,
  407. powFraction = 10;
  408. value = Math.abs(value);
  409. if (decimalIndex != -1) {
  410. fraction = _locale.number.decimalSeparator;
  411. if (maxFraction > 0) {
  412. roundFactor = 1000;
  413. powFraction = Math.pow(powFraction, maxFraction);
  414. var tempRound = Math.round(parseInt(value * powFraction * roundFactor -
  415. Math.round(value) * powFraction * roundFactor, 10) / roundFactor),
  416. tempFraction = String(tempRound < 0 ? Math.round(parseInt(value * powFraction * roundFactor -
  417. parseInt(value, 10) * powFraction * roundFactor, 10) / roundFactor) : tempRound),
  418. parts = value.toString().split('.');
  419. if (typeof parts[1] != UNDEFINED) {
  420. for (i = 0; i < maxFraction; i++) {
  421. if (parts[1].substr(i, 1) == '0' && i < maxFraction - 1 &&
  422. tempFraction.length != maxFraction) {
  423. tempFraction = '0' + tempFraction;
  424. } else {
  425. break;
  426. }
  427. }
  428. }
  429. for (i = 0; i < (maxFraction - fraction.length); i++) {
  430. tempFraction += '0';
  431. }
  432. var symbol,
  433. formattedFraction = '';
  434. for (i = 0; i < tempFraction.length; i++) {
  435. symbol = tempFraction.substr(i, 1);
  436. if (i >= minFraction && symbol == '0' && /^0*$/.test(tempFraction.substr(i+1))) {
  437. break;
  438. }
  439. formattedFraction += symbol;
  440. }
  441. fraction += formattedFraction;
  442. }
  443. if (fraction == _locale.number.decimalSeparator) {
  444. fraction = '';
  445. }
  446. }
  447. if (decimalIndex !== 0) {
  448. if (fraction != '') {
  449. integer = String(parseInt(Math.round(value * powFraction) / powFraction, 10));
  450. } else {
  451. integer = String(Math.round(value));
  452. }
  453. var grouping = _locale.number.groupingSeparator,
  454. groupingSize = 0;
  455. if (groupingIndex != -1) {
  456. if (decimalIndex != -1) {
  457. groupingSize = decimalIndex - groupingIndex;
  458. } else {
  459. groupingSize = format.length - groupingIndex;
  460. }
  461. groupingSize--;
  462. }
  463. if (groupingSize > 0) {
  464. var count = 0,
  465. formattedInteger = '';
  466. i = integer.length;
  467. while (i--) {
  468. if (count !== 0 && count % groupingSize === 0) {
  469. formattedInteger = grouping + formattedInteger;
  470. }
  471. formattedInteger = integer.substr(i, 1) + formattedInteger;
  472. count++;
  473. }
  474. integer = formattedInteger;
  475. }
  476. var maxInteger, maxRegExp = /#|,/g;
  477. if (decimalIndex != -1) {
  478. maxInteger = format.substr(0, decimalIndex).replace(maxRegExp, '').length;
  479. } else {
  480. maxInteger = format.replace(maxRegExp, '').length;
  481. }
  482. var tempInteger = integer.length;
  483. for (i = tempInteger; i < maxInteger; i++) {
  484. integer = '0' + integer;
  485. }
  486. }
  487. result = integer + fraction;
  488. return (negative ? '-' : '') + result;
  489. }
  490. }
  491. };
  492. })();
  493. }));