Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

800 rader
25 KiB

  1. /**
  2. * Copyright (c)2005-2009 Matt Kruse (javascripttoolbox.com)
  3. * Dual licensed under the MIT and GPL licenses.
  4. * This basically means you can use this code however you want for
  5. */
  6. /*
  7. Date functions
  8. These functions are used to parse, format, and manipulate Date objects.
  9. See documentation and examples at http://www.JavascriptToolbox.com/lib/date/
  10. */
  11. Date.$VERSION = 1.02;
  12. // Utility function to append a 0 to single-digit numbers
  13. Date.LZ = function(x) {return(x<0||x>9?"":"0")+x};
  14. // Full month names. Change this for local month names
  15. // Date.monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  16. Date.monthNames = new Array('一月','二月','三月','四月','五月','J六月','七月','八月','九月','十月','十一月','十二月');
  17. // Month abbreviations. Change this for local month names
  18. // Date.monthAbbreviations = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  19. Date.monthAbbreviations = new Array('一月','二月','三月','四月','五月','J六月','七月','八月','九月','十月','十一月','十二月');
  20. // Full day names. Change this for local month names
  21. // Date.dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  22. Date.dayNames = new Array('日','一','二','三','四','五','六');
  23. // Day abbreviations. Change this for local month names
  24. // Date.dayAbbreviations = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  25. Date.dayAbbreviations = new Array('日','一','二','三','四','五','六');
  26. // Used for parsing ambiguous dates like 1/2/2000 - default to preferring 'American' format meaning Jan 2.
  27. // Set to false to prefer 'European' format meaning Feb 1
  28. Date.preferAmericanFormat = true;
  29. // Set to 0=SUn for American 1=Mon for european
  30. Date.firstDayOfWeek = 0;
  31. //default
  32. Date.defaultFormat="dd/MM/yyyy";
  33. // If the getFullYear() method is not defined, create it
  34. if (!Date.prototype.getFullYear) {
  35. Date.prototype.getFullYear = function() { var yy=this.getYear(); return (yy<1900?yy+1900:yy); } ;
  36. }
  37. // Parse a string and convert it to a Date object.
  38. // If no format is passed, try a list of common formats.
  39. // If string cannot be parsed, return null.
  40. // Avoids regular expressions to be more portable.
  41. Date.parseString = function(val, format,lenient) {
  42. // If no format is specified, try a few common formats
  43. if (typeof(format)=="undefined" || format==null || format=="") {
  44. var generalFormats=new Array(Date.defaultFormat,'y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d','MMM-d','d-MMM');
  45. var monthFirst=new Array('M/d/y','M-d-y','M.d.y','M/d','M-d');
  46. var dateFirst =new Array('d/M/y','d-M-y','d.M.y','d/M','d-M');
  47. var checkList=new Array(generalFormats,Date.preferAmericanFormat?monthFirst:dateFirst,Date.preferAmericanFormat?dateFirst:monthFirst);
  48. for (var i=0; i<checkList.length; i++) {
  49. var l=checkList[i];
  50. for (var j=0; j<l.length; j++) {
  51. var d=Date.parseString(val,l[j]);
  52. if (d!=null) {
  53. return d;
  54. }
  55. }
  56. }
  57. return null;
  58. };
  59. this.isInteger = function(val) {
  60. for (var i=0; i < val.length; i++) {
  61. if ("1234567890".indexOf(val.charAt(i))==-1) {
  62. return false;
  63. }
  64. }
  65. return true;
  66. };
  67. this.getInt = function(str,i,minlength,maxlength) {
  68. for (var x=maxlength; x>=minlength; x--) {
  69. var token=str.substring(i,i+x);
  70. if (token.length < minlength) {
  71. return null;
  72. }
  73. if (this.isInteger(token)) {
  74. return token;
  75. }
  76. }
  77. return null;
  78. };
  79. this.decodeShortcut=function(str){
  80. str=str?str:""; // just in case
  81. var dateUpper = str.trim().toUpperCase();
  82. var ret=new Date();
  83. ret.clearTime();
  84. if (["NOW","N"].indexOf(dateUpper)>=0) {
  85. ret= new Date();
  86. } else if (["TODAY","T"].indexOf(dateUpper)>=0) {
  87. //do nothing
  88. } else if (["YESTERDAY","Y"].indexOf(dateUpper)>=0) {
  89. ret.setDate(ret.getDate()-1);
  90. } else if (["TOMORROW","TO"].indexOf(dateUpper)>=0) {
  91. ret.setDate(ret.getDate()+1);
  92. } else if (["W", "TW", "WEEK", "THISWEEK", "WEEKSTART", "THISWEEKSTART"].indexOf(dateUpper)>=0) {
  93. ret.setFirstDayOfThisWeek();
  94. } else if (["LW", "LASTWEEK", "LASTWEEKSTART"].indexOf(dateUpper)>=0) {
  95. ret.setFirstDayOfThisWeek();
  96. ret.setDate(ret.getDate()-7);
  97. } else if (["NW", "NEXTWEEK", "NEXTWEEKSTART"].indexOf(dateUpper)>=0) {
  98. ret.setFirstDayOfThisWeek();
  99. ret.setDate(ret.getDate()+7);
  100. } else if (["M", "TM", "MONTH", "THISMONTH", "MONTHSTART", "THISMONTHSTART"].indexOf(dateUpper)>=0) {
  101. ret.setDate(1);
  102. } else if (["LM", "LASTMONTH", "LASTMONTHSTART"].indexOf(dateUpper)>=0) {
  103. ret.setDate(1);
  104. ret.setMonth(ret.getMonth()-1);
  105. } else if (["NM", "NEXTMONTH", "NEXTMONTHSTART"].indexOf(dateUpper)>=0) {
  106. ret.setDate(1);
  107. ret.setMonth(ret.getMonth()+1);
  108. } else if (["Q", "TQ", "QUARTER", "THISQUARTER", "QUARTERSTART", "THISQUARTERSTART"].indexOf(dateUpper)>=0) {
  109. ret.setDate(1);
  110. ret.setMonth(Math.floor((ret.getMonth()) / 3) * 3);
  111. } else if (["LQ", "LASTQUARTER", "LASTQUARTERSTART"].indexOf(dateUpper)>=0) {
  112. ret.setDate(1);
  113. ret.setMonth(Math.floor((ret.getMonth()) / 3) * 3-3);
  114. } else if (["NQ", "NEXTQUARTER", "NEXTQUARTERSTART"].indexOf(dateUpper)>=0) {
  115. ret.setDate(1);
  116. ret.setMonth(Math.floor((ret.getMonth()) / 3) * 3+3);
  117. } else if (/^-?[0-9]+[DWMY]$/.test(dateUpper)) {
  118. var lastOne = dateUpper.substr(dateUpper.length - 1);
  119. var val = parseInt(dateUpper.substr(0, dateUpper.length - 1));
  120. if (lastOne=="W")
  121. ret.setDate(ret.getDate()+val*7 );
  122. else if (lastOne=="M")
  123. ret.setMonth(ret.getMonth()+val );
  124. else if (lastOne=="Y")
  125. ret.setYear(ret.getYear()+val );
  126. } else {
  127. ret=undefined;
  128. }
  129. return ret;
  130. };
  131. var ret=this.decodeShortcut(val);
  132. if (ret)
  133. return ret;
  134. this._getDate = function(val, format) {
  135. val = val + "";
  136. format = format + "";
  137. var i_val = 0;
  138. var i_format = 0;
  139. var c = "";
  140. var token = "";
  141. var token2 = "";
  142. var x,y;
  143. var year = new Date().getFullYear();
  144. var month = 1;
  145. var date = 1;
  146. var hh = 0;
  147. var mm = 0;
  148. var ss = 0;
  149. var ampm = "";
  150. while (i_format < format.length) {
  151. // Get next token from format string
  152. c = format.charAt(i_format);
  153. token = "";
  154. while ((format.charAt(i_format) == c) && (i_format < format.length)) {
  155. token += format.charAt(i_format++);
  156. }
  157. // Extract contents of value based on format token
  158. if (token == "yyyy" || token == "yy" || token == "y") {
  159. if (token == "yyyy") {
  160. x = 4;
  161. y = 4;
  162. }
  163. if (token == "yy") {
  164. x = 2;
  165. y = 2;
  166. }
  167. if (token == "y") {
  168. x = 2;
  169. y = 4;
  170. }
  171. year = this.getInt(val, i_val, x, y);
  172. if (year == null) {
  173. return null;
  174. }
  175. i_val += year.length;
  176. if (year.length == 2) {
  177. if (year > 70) {
  178. year = 1900 + (year - 0);
  179. }
  180. else {
  181. year = 2000 + (year - 0);
  182. }
  183. }
  184. // } else if (token=="MMM" || token=="NNN"){
  185. } else if (token == "MMM" || token == "MMMM") {
  186. month = 0;
  187. var names = (token == "MMMM" ? (Date.monthNames.concat(Date.monthAbbreviations)) : Date.monthAbbreviations);
  188. for (var i = 0; i < names.length; i++) {
  189. var month_name = names[i];
  190. if (val.substring(i_val, i_val + month_name.length).toLowerCase() == month_name.toLowerCase()) {
  191. month = (i % 12) + 1;
  192. i_val += month_name.length;
  193. break;
  194. }
  195. }
  196. if ((month < 1) || (month > 12)) {
  197. return null;
  198. }
  199. } else if (token == "E" || token == "EE" || token == "EEE" || token == "EEEE") {
  200. var names = (token == "EEEE" ? Date.dayNames : Date.dayAbbreviations);
  201. for (var i = 0; i < names.length; i++) {
  202. var day_name = names[i];
  203. if (val.substring(i_val, i_val + day_name.length).toLowerCase() == day_name.toLowerCase()) {
  204. i_val += day_name.length;
  205. break;
  206. }
  207. }
  208. } else if (token == "MM" || token == "M") {
  209. month = this.getInt(val, i_val, token.length, 2);
  210. if (month == null || (month < 1) || (month > 12)) {
  211. return null;
  212. }
  213. i_val += month.length;
  214. } else if (token == "dd" || token == "d") {
  215. date = this.getInt(val, i_val, token.length, 2);
  216. if (date == null || (date < 1) || (date > 31)) {
  217. return null;
  218. }
  219. i_val += date.length;
  220. } else if (token == "hh" || token == "h") {
  221. hh = this.getInt(val, i_val, token.length, 2);
  222. if (hh == null || (hh < 1) || (hh > 12)) {
  223. return null;
  224. }
  225. i_val += hh.length;
  226. } else if (token == "HH" || token == "H") {
  227. hh = this.getInt(val, i_val, token.length, 2);
  228. if (hh == null || (hh < 0) || (hh > 23)) {
  229. return null;
  230. }
  231. i_val += hh.length;
  232. } else if (token == "KK" || token == "K") {
  233. hh = this.getInt(val, i_val, token.length, 2);
  234. if (hh == null || (hh < 0) || (hh > 11)) {
  235. return null;
  236. }
  237. i_val += hh.length;
  238. hh++;
  239. } else if (token == "kk" || token == "k") {
  240. hh = this.getInt(val, i_val, token.length, 2);
  241. if (hh == null || (hh < 1) || (hh > 24)) {
  242. return null;
  243. }
  244. i_val += hh.length;
  245. hh--;
  246. } else if (token == "mm" || token == "m") {
  247. mm = this.getInt(val, i_val, token.length, 2);
  248. if (mm == null || (mm < 0) || (mm > 59)) {
  249. return null;
  250. }
  251. i_val += mm.length;
  252. } else if (token == "ss" || token == "s") {
  253. ss = this.getInt(val, i_val, token.length, 2);
  254. if (ss == null || (ss < 0) || (ss > 59)) {
  255. return null;
  256. }
  257. i_val += ss.length;
  258. } else if (token == "a") {
  259. if (val.substring(i_val, i_val + 2).toLowerCase() == "am") {
  260. ampm = "AM";
  261. } else if (val.substring(i_val, i_val + 2).toLowerCase() == "pm") {
  262. ampm = "PM";
  263. } else {
  264. return null;
  265. }
  266. i_val += 2;
  267. } else {
  268. if (val.substring(i_val, i_val + token.length) != token) {
  269. return null;
  270. } else {
  271. i_val += token.length;
  272. }
  273. }
  274. }
  275. // If there are any trailing characters left in the value, it doesn't match
  276. if (i_val != val.length) {
  277. return null;
  278. }
  279. // Is date valid for month?
  280. if (month == 2) {
  281. // Check for leap year
  282. if (( (year % 4 == 0) && (year % 100 != 0) ) || (year % 400 == 0)) { // leap year
  283. if (date > 29) {
  284. return null;
  285. }
  286. } else {
  287. if (date > 28) {
  288. return null;
  289. }
  290. }
  291. }
  292. if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
  293. if (date > 30) {
  294. return null;
  295. }
  296. }
  297. // Correct hours value
  298. if (hh < 12 && ampm == "PM") {
  299. hh = hh - 0 + 12;
  300. }
  301. else if (hh > 11 && ampm == "AM") {
  302. hh -= 12;
  303. }
  304. return new Date(year, month - 1, date, hh, mm, ss);
  305. };
  306. var theDate=this._getDate(val, format);
  307. if (!theDate && lenient){
  308. //try with short format
  309. var f=format.replace("MMMM","M").replace("MMM","M").replace("MM","M")
  310. .replace("yyyy","y").replace("yyy","y").replace("yy","y")
  311. .replace("dd","d");
  312. //console.debug("second round with format "+f);
  313. return this._getDate(val, f);
  314. } else {
  315. return theDate;
  316. }
  317. };
  318. // Check if a date string is valid
  319. Date.isValid = function(val,format,lenient) {
  320. return (Date.parseString(val,format,lenient) != null);
  321. };
  322. // Check if a date object is before another date object
  323. Date.prototype.isBefore = function(date2) {
  324. if (date2==null) {
  325. return false;
  326. }
  327. return (this.getTime()<date2.getTime());
  328. };
  329. // Check if a date object is after another date object
  330. Date.prototype.isAfter = function(date2) {
  331. if (date2==null) {
  332. return false;
  333. }
  334. return (this.getTime()>date2.getTime());
  335. };
  336. // Check if two date objects have equal dates and times
  337. Date.prototype.equals = function(date2) {
  338. if (date2==null) {
  339. return false;
  340. }
  341. return (this.getTime()==date2.getTime());
  342. };
  343. // Check if two date objects have equal dates, disregarding times
  344. Date.prototype.equalsIgnoreTime = function(date2) {
  345. if (date2==null) {
  346. return false;
  347. }
  348. var d1 = new Date(this.getTime()).clearTime();
  349. var d2 = new Date(date2.getTime()).clearTime();
  350. return (d1.getTime()==d2.getTime());
  351. };
  352. /**
  353. * Get week number in the year.
  354. */
  355. Date.prototype.getWeekNumber = function() {
  356. var d = new Date(+this);
  357. d.setHours(0,0,0,0);
  358. d.setDate(d.getDate()+4-(d.getDay()||7));
  359. return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
  360. };
  361. // Format a date into a string using a given format string
  362. Date.prototype.format = function(format) {
  363. if (!format)
  364. format=Date.defaultFormat;
  365. format=format+"";
  366. var result="";
  367. var i_format=0;
  368. var c="";
  369. var token="";
  370. var y=this.getFullYear()+"";
  371. var M=this.getMonth()+1;
  372. var d=this.getDate();
  373. var E=this.getDay();
  374. var H=this.getHours();
  375. var m=this.getMinutes();
  376. var s=this.getSeconds();
  377. var w=this.getWeekNumber();
  378. // Convert real date parts into formatted versions
  379. var value=new Object();
  380. if (y.length < 4) {
  381. y=""+(+y+1900);
  382. }
  383. value["y"]=""+y;
  384. value["yyyy"]=y;
  385. value["yy"]=y.substring(2,4);
  386. value["M"]=M;
  387. value["MM"]=Date.LZ(M);
  388. value["MMM"]=Date.monthAbbreviations[M-1];
  389. value["MMMM"]=Date.monthNames[M-1];
  390. value["d"]=d;
  391. value["dd"]=Date.LZ(d);
  392. value["E"]=Date.dayAbbreviations[E];
  393. value["EE"]=Date.dayAbbreviations[E];
  394. value["EEE"]=Date.dayAbbreviations[E];
  395. value["EEEE"]=Date.dayNames[E];
  396. value["H"]=H;
  397. value["HH"]=Date.LZ(H);
  398. value["w"]=w;
  399. value["ww"]=Date.LZ(w);
  400. if (H==0){
  401. value["h"]=12;
  402. }
  403. else if (H>12){
  404. value["h"]=H-12;
  405. }
  406. else {
  407. value["h"]=H;
  408. }
  409. value["hh"]=Date.LZ(value["h"]);
  410. value["K"]=value["h"]-1;
  411. value["k"]=value["H"]+1;
  412. value["KK"]=Date.LZ(value["K"]);
  413. value["kk"]=Date.LZ(value["k"]);
  414. if (H > 11) {
  415. value["a"]="PM";
  416. }
  417. else {
  418. value["a"]="AM";
  419. }
  420. value["m"]=m;
  421. value["mm"]=Date.LZ(m);
  422. value["s"]=s;
  423. value["ss"]=Date.LZ(s);
  424. while (i_format < format.length) {
  425. c=format.charAt(i_format);
  426. token="";
  427. while ((format.charAt(i_format)==c) && (i_format < format.length)) {
  428. token += format.charAt(i_format++);
  429. }
  430. if (typeof(value[token])!="undefined") {
  431. result=result + value[token];
  432. }
  433. else {
  434. result=result + token;
  435. }
  436. }
  437. return result;
  438. };
  439. // Get the full name of the day for a date
  440. Date.prototype.getDayName = function() {
  441. return Date.dayNames[this.getDay()];
  442. };
  443. // Get the abbreviation of the day for a date
  444. Date.prototype.getDayAbbreviation = function() {
  445. return Date.dayAbbreviations[this.getDay()];
  446. };
  447. // Get the full name of the month for a date
  448. Date.prototype.getMonthName = function() {
  449. return Date.monthNames[this.getMonth()];
  450. };
  451. // Get the abbreviation of the month for a date
  452. Date.prototype.getMonthAbbreviation = function() {
  453. return Date.monthAbbreviations[this.getMonth()];
  454. };
  455. // Clear all time information in a date object
  456. Date.prototype.clearTime = function() {
  457. this.setHours(0);
  458. this.setMinutes(0);
  459. this.setSeconds(0);
  460. this.setMilliseconds(0);
  461. return this;
  462. };
  463. // Add an amount of time to a date. Negative numbers can be passed to subtract time.
  464. Date.prototype.add = function(interval, number) {
  465. if (typeof(interval)=="undefined" || interval==null || typeof(number)=="undefined" || number==null) {
  466. return this;
  467. }
  468. number = +number;
  469. if (interval=='y') { // year
  470. this.setFullYear(this.getFullYear()+number);
  471. } else if (interval=='M') { // Month
  472. this.setMonth(this.getMonth()+number);
  473. } else if (interval=='d') { // Day
  474. this.setDate(this.getDate()+number);
  475. } else if (interval=='w') { // Week
  476. this.setDate(this.getDate()+number*7);
  477. } else if (interval=='h') { // Hour
  478. this.setHours(this.getHours() + number);
  479. } else if (interval=='m') { // Minute
  480. this.setMinutes(this.getMinutes() + number);
  481. } else if (interval=='s') { // Second
  482. this.setSeconds(this.getSeconds() + number);
  483. }
  484. return this;
  485. };
  486. Date.prototype.toInt = function () {
  487. return this.getFullYear()*10000+(this.getMonth()+1)*100+this.getDate();
  488. };
  489. Date.fromInt=function (dateInt){
  490. var year = parseInt(dateInt/10000);
  491. var month = parseInt((dateInt-year*10000)/100);
  492. var day = parseInt(dateInt-year*10000-month*100);
  493. return new Date(year,month-1,day,12,00,00);
  494. };
  495. Date.prototype.isHoliday=function(){
  496. return isHoliday(this);
  497. };
  498. Date.prototype.isToday=function(){
  499. return this.toInt()==new Date().toInt();
  500. };
  501. Date.prototype.incrementDateByWorkingDays=function (days) {
  502. //console.debug("incrementDateByWorkingDays start ",d,days)
  503. var q = Math.abs(days);
  504. while (q > 0) {
  505. this.setDate(this.getDate() + (days > 0 ? 1 : -1));
  506. if (!this.isHoliday())
  507. q--;
  508. }
  509. return this;
  510. };
  511. Date.prototype.distanceInDays= function (toDate){
  512. // Discard the time and time-zone information.
  513. var utc1 = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate());
  514. var utc2 = Date.UTC(toDate.getFullYear(), toDate.getMonth(), toDate.getDate());
  515. return Math.floor((utc2 - utc1) / (3600000*24));
  516. };
  517. //low performances in case of long distance
  518. /*Date.prototype.distanceInWorkingDays= function (toDate){
  519. var pos = new Date(this.getTime());
  520. pos.setHours(23, 59, 59, 999);
  521. var days = 0;
  522. var nd=new Date(toDate.getTime());
  523. nd.setHours(23, 59, 59, 999);
  524. var end=nd.getTime();
  525. while (pos.getTime() <= end) {
  526. days = days + (isHoliday(pos) ? 0 : 1);
  527. pos.setDate(pos.getDate() + 1);
  528. }
  529. return days;
  530. };*/
  531. //low performances in case of long distance
  532. // bicch 22/4/2016: modificato per far ritornare anche valori negativi, così come la controparte Java in CompanyCalendar.
  533. // attenzione che prima tornava 1 per due date uguali adesso torna 0
  534. Date.prototype.distanceInWorkingDays= function (toDate){
  535. var pos = new Date(Math.min(this,toDate));
  536. pos.setHours(12, 0, 0, 0);
  537. var days = 0;
  538. var nd=new Date(Math.max(this,toDate));
  539. nd.setHours(12, 0,0, 0);
  540. while (pos < nd) {
  541. days = days + (isHoliday(pos) ? 0 : 1);
  542. pos.setDate(pos.getDate() + 1);
  543. }
  544. days=days*(this>toDate?-1:1);
  545. //console.debug("distanceInWorkingDays",this,toDate,days);
  546. return days;
  547. };
  548. Date.prototype.setFirstDayOfThisWeek= function (firstDayOfWeek){
  549. if (!firstDayOfWeek)
  550. firstDayOfWeek=Date.firstDayOfWeek;
  551. this.setDate(this.getDate() - this.getDay() +firstDayOfWeek - (this.getDay()==0 && firstDayOfWeek!=0 ?7:0));
  552. return this;
  553. };
  554. /* ----- millis format --------- */
  555. /**
  556. * @param str - Striga da riempire
  557. * @param len - Numero totale di caratteri, comprensivo degli "zeri"
  558. * @param ch - Carattere usato per riempire
  559. */
  560. function pad(str, len, ch) {
  561. if ((str + "").length < len) {
  562. return new Array(len - ('' + str).length + 1).join(ch) + str;
  563. } else {
  564. return str
  565. }
  566. }
  567. function getMillisInHours(millis) {
  568. if (!millis)
  569. return "";
  570. var hour = Math.floor(millis / 3600000);
  571. return ( millis >= 0 ? "" : "-") + pad(hour, 1, "0");
  572. }
  573. function getMillisInHoursMinutes(millis) {
  574. if (typeof(millis) != "number")
  575. return "";
  576. var sgn = millis >= 0 ? 1 : -1;
  577. millis = Math.abs(millis);
  578. var hour = Math.floor(millis / 3600000);
  579. var min = Math.floor((millis % 3600000) / 60000);
  580. return (sgn > 0 ? "" : "-") + pad(hour, 1, "0") + ":" + pad(min, 2, "0");
  581. }
  582. function getMillisInDaysHoursMinutes(millis) {
  583. if (!millis)
  584. return "";
  585. // millisInWorkingDay is set on partHeaderFooter
  586. var sgn = millis >= 0 ? 1 : -1;
  587. millis = Math.abs(millis);
  588. var days = Math.floor(millis / millisInWorkingDay);
  589. var hour = Math.floor((millis % millisInWorkingDay) / 3600000);
  590. var min = Math.floor((millis - days * millisInWorkingDay - hour * 3600000) / 60000);
  591. return (sgn >= 0 ? "" : "-") + (days > 0 ? days + " " : "") + pad(hour, 1, "0") + ":" + pad(min, 2, "0");
  592. }
  593. function millisFromHourMinute(stringHourMinutes) { //All this format are valid: "12:58" "13.75" "63635676000" (this is already in milliseconds)
  594. var result = 0;
  595. stringHourMinutes.replace(",", ".");
  596. var semiColSeparator = stringHourMinutes.indexOf(":");
  597. var dotSeparator = stringHourMinutes.indexOf(".");
  598. if (semiColSeparator < 0 && dotSeparator < 0 && stringHourMinutes.length > 5) {
  599. return parseInt(stringHourMinutes, 10); //already in millis
  600. } else {
  601. if (dotSeparator > -1) {
  602. var d = parseFloat(stringHourMinutes);
  603. result = d * 3600000;
  604. } else {
  605. var hour = 0;
  606. var minute = 0;
  607. if (semiColSeparator == -1)
  608. hour = parseInt(stringHourMinutes, 10);
  609. else {
  610. hour = parseInt(stringHourMinutes.substring(0, semiColSeparator), 10);
  611. minute = parseInt(stringHourMinutes.substring(semiColSeparator + 1), 10);
  612. }
  613. result = hour * 3600000 + minute * 60000;
  614. }
  615. if (typeof(result) != "number")
  616. result = NaN;
  617. return result;
  618. }
  619. }
  620. /**
  621. * @param string "3y 4d", "4D:08:10", "12M/3d", "1.5D", "2H4D", "3M4d,2h", "12:30", "11", "3", "1.5", "2m/3D", "12/3d", "1234"
  622. * by default 2 means 2 hours 1.5 means 1:30
  623. * @param considerWorkingdays if true day length is from global.properties CompanyCalendar.MILLIS_IN_WORKING_DAY otherwise in 24
  624. * @return milliseconds. 0 if invalid string
  625. */
  626. function millisFromString(string, considerWorkingdays) {
  627. if (!string)
  628. return 0;
  629. //var regex = new RegExp("(\\d+[Yy])|(\\d+[M])|(\\d+[Ww])|(\\d+[Dd])|(\\d+[Hh])|(\\d+[m])|(\\d+[Ss])|(\\d+:\\d+)|(:\\d+)|(\\d*[\\.,]\\d+)|(\\d+)", "g"); // bicch 14/1/16 supporto per 1.5d
  630. var regex = new RegExp("([0-9\\.,]+[Yy])|([0-9\\.,]+[M])|([0-9\\.,]+[Ww])|([0-9\\.,]+[Dd])|([0-9\\.,]+[Hh])|([0-9\\.,]+[m])|([0-9\\.,]+[Ss])|(\\d+:\\d+)|(:\\d+)|(\\d*[\\.,]\\d+)|(\\d+)", "g");
  631. var matcher = regex.exec(string);
  632. var totMillis = 0;
  633. if (!matcher)
  634. return NaN;
  635. while (matcher != null) {
  636. for (var i = 1; i < matcher.length; i++) {
  637. var match = matcher[i];
  638. if (match) {
  639. var number = 0;
  640. try {
  641. //number = parseInt(match); // bicch 14/1/16 supporto per 1.5d
  642. number = parseFloat(match.replace(',','.'));
  643. } catch (e) {
  644. }
  645. if (i == 1) { // years
  646. totMillis = totMillis + number * (considerWorkingdays ? millisInWorkingDay * workingDaysPerWeek * 52 : 3600000 * 24 * 365);
  647. } else if (i == 2) { // months
  648. totMillis = totMillis + number * (considerWorkingdays ? millisInWorkingDay * workingDaysPerWeek * 4 : 3600000 * 24 * 30);
  649. } else if (i == 3) { // weeks
  650. totMillis = totMillis + number * (considerWorkingdays ? millisInWorkingDay * workingDaysPerWeek : 3600000 * 24 * 7);
  651. } else if (i == 4) { // days
  652. totMillis = totMillis + number * (considerWorkingdays ? millisInWorkingDay : 3600000 * 24);
  653. } else if (i == 5) { // hours
  654. totMillis = totMillis + number * 3600000;
  655. } else if (i == 6) { // minutes
  656. totMillis = totMillis + number * 60000;
  657. } else if (i == 7) { // seconds
  658. totMillis = totMillis + number * 1000;
  659. } else if (i == 8) { // hour:minutes
  660. totMillis = totMillis + millisFromHourMinute(match);
  661. } else if (i == 9) { // :minutes
  662. totMillis = totMillis + millisFromHourMinute(match);
  663. } else if (i == 10) { // hour.minutes
  664. totMillis = totMillis + millisFromHourMinute(match);
  665. } else if (i == 11) { // hours
  666. totMillis = totMillis + number * 3600000;
  667. }
  668. }
  669. }
  670. matcher = regex.exec(string);
  671. }
  672. return totMillis;
  673. }
  674. /**
  675. * @param string "3y 4d", "4D:08:10", "12M/3d", "2H4D", "3M4d,2h", "12:30", "11", "3", "1.5", "2m/3D", "12/3d", "1234"
  676. * by default 2 means 2 hours 1.5 means 1:30
  677. * @param considerWorkingdays if true day length is from global.properties CompanyCalendar.MILLIS_IN_WORKING_DAY otherwise in 24
  678. * @return milliseconds. 0 if invalid string
  679. */
  680. function daysFromString(string, considerWorkingdays) {
  681. if (!string)
  682. return undefined;
  683. //var regex = new RegExp("(\\d+[Yy])|(\\d+[Mm])|(\\d+[Ww])|(\\d+[Dd])|(\\d*[\\.,]\\d+)|(\\d+)", "g"); // bicch 14/1/16 supporto per 1.5d
  684. var regex = new RegExp("([0-9\\.,]+[Yy])|([0-9\\.,]+[Mm])|([0-9\\.,]+[Ww])|([0-9\\.,]+[Dd])|(\\d*[\\.,]\\d+)|(\\d+)", "g");
  685. var matcher = regex.exec(string);
  686. var totDays = 0;
  687. if (!matcher)
  688. return NaN;
  689. while (matcher != null) {
  690. for (var i = 1; i < matcher.length; i++) {
  691. var match = matcher[i];
  692. if (match) {
  693. var number = 0;
  694. try {
  695. number = parseInt(match);// bicch 14/1/16 supporto per 1.5d
  696. number = parseFloat(match.replace(',','.'));
  697. } catch (e) {
  698. }
  699. if (i == 1) { // years
  700. totDays = totDays + number * (considerWorkingdays ? workingDaysPerWeek * 52 : 365);
  701. } else if (i == 2) { // months
  702. totDays = totDays + number * (considerWorkingdays ? workingDaysPerWeek * 4 : 30);
  703. } else if (i == 3) { // weeks
  704. totDays = totDays + number * (considerWorkingdays ? workingDaysPerWeek : 7);
  705. } else if (i == 4) { // days
  706. totDays = totDays + number;
  707. } else if (i == 5) { // days.minutes
  708. totDays = totDays + number;
  709. } else if (i == 6) { // days
  710. totDays = totDays + number;
  711. }
  712. }
  713. }
  714. matcher = regex.exec(string);
  715. }
  716. return parseInt(totDays);
  717. }