tools.wxs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. var numToArray = function (num) {
  2. var arr = [];
  3. for (var i = 0; i < num; i++) {
  4. arr[i] = i;
  5. }
  6. return arr;
  7. }
  8. var formatNumber = function (n) {
  9. n = n.toString()
  10. return n[1] ? n : '0' + n
  11. }
  12. /**
  13. * 时间戳格式化时间
  14. */
  15. var formatTime = function (datetime) {
  16. if (!datetime) return '';
  17. var date = getDate(datetime * 1000);
  18. var year = date.getFullYear()
  19. var month = date.getMonth() + 1
  20. var day = date.getDate()
  21. var hour = date.getHours()
  22. var minute = date.getMinutes()
  23. var second = date.getSeconds()
  24. return year + '-' + formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' + formatNumber(minute) + ':' + formatNumber(second);
  25. }
  26. /**
  27. * 时间戳格式化时间 不带秒
  28. */
  29. var formatTimeNoSecond = function (datetime) {
  30. if (!datetime) return '';
  31. var date = getDate(datetime * 1000);
  32. var year = date.getFullYear()
  33. var month = date.getMonth() + 1
  34. var day = date.getDate()
  35. var hour = date.getHours()
  36. var minute = date.getMinutes()
  37. var second = date.getSeconds()
  38. return year + '-' + formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' + formatNumber(minute);
  39. }
  40. // 时间戳格式化 返回日期
  41. var formatGetDate = function (datetime) {
  42. var date = getDate(datetime * 1000);
  43. var year = date.getFullYear()
  44. var month = date.getMonth() + 1
  45. var day = date.getDate()
  46. return year + '-' + formatNumber(month) + '-' + formatNumber(day);
  47. }
  48. /**
  49. * 隐藏字符串 多余字符使用*替代
  50. */
  51. var maskStr = function (str, replace, len) {
  52. if (typeof str != 'string')
  53. return '';
  54. if(str.length < len) return str;
  55. return str.substring(0, len) + replace + replace + replace;
  56. }
  57. /**
  58. * 将字符串分割成数组
  59. */
  60. var splitx = function (str, splitstr) {
  61. if (typeof str != 'string')
  62. return [];
  63. return str.split(splitstr);
  64. }
  65. var toFixed = function(num, n) {
  66. return Math.floor(num * Math.pow(10, n)) / Math.pow(10, n)
  67. }
  68. var showMoney = function(m) {
  69. var _n = m / 1000
  70. if(_n <=1 ) {
  71. return m
  72. }
  73. if (_n > 1 && _n < 10) {
  74. return _n.toFixed(1) + '千'
  75. }
  76. _n = m / 10000
  77. return _n.toFixed(1) + '万'
  78. }
  79. function formatSeconds(value) {
  80. var theTime = parseInt(value);// 需要转换的时间秒
  81. var theTime1 = 0;// 分
  82. var theTime2 = 0;// 小时
  83. var theTime3 = 0;// 天
  84. if (theTime === 0) return [parseInt(theTime), "sec", 'S'];
  85. if (theTime > 60) {
  86. theTime1 = parseInt(theTime / 60);
  87. theTime = parseInt(theTime % 60);
  88. if (theTime1 > 60) {
  89. theTime2 = parseInt(theTime1 / 60);
  90. theTime1 = parseInt(theTime1 % 60);
  91. if (theTime2 > 24) {
  92. //大于24小时
  93. theTime3 = parseInt(theTime2 / 24);
  94. theTime2 = parseInt(theTime2 % 24);
  95. }
  96. }
  97. }
  98. var result = ['', ''];
  99. if (theTime > 0) {
  100. result = [parseInt(theTime) , "sec", 'S'];
  101. }
  102. if (theTime1 > 0) {
  103. result = [parseInt(theTime1), "min", 'M'].concat(result);
  104. }
  105. if (theTime2 > 0) {
  106. result = [parseInt(theTime2), "hour", 'H'].concat(result);
  107. }
  108. if (theTime3 > 0) {
  109. result = [parseInt(theTime3), "day", 'D'].concat(result);
  110. }
  111. return result;
  112. }
  113. var formatHourSeconds = function (time) {
  114. var sec = parseInt(time % 60);
  115. var min = parseInt(parseInt(time / 60) % 60);
  116. var hour = parseInt(parseInt(time / 60) / 60);
  117. return formatNumber(hour) + ':' + formatNumber(min) + ':' + formatNumber(sec);
  118. }
  119. var filterHalf = function (l) {
  120. return l.filter(function (p, i) { return i % 2 == 1});
  121. }
  122. var max = function (arr) {
  123. return [...arr].sort(function (a, b) { return b - a })[0];
  124. }
  125. var min = function (arr) {
  126. return [...arr].sort(function (a, b) { return a - b })[0];
  127. }
  128. var getOption = function (option, value) {
  129. return option.filter(function (p, i) { return p.value == value })[0]
  130. }
  131. var getOptionIndex = function (option, value) {
  132. var i
  133. for (i = 0; i < option.length; i++) {
  134. if (option[i].value == value) {
  135. return i
  136. }
  137. }
  138. }
  139. module.exports = {
  140. numToArray: numToArray,
  141. formatTime: formatTime,
  142. formatGetDate: formatGetDate,
  143. maskStr: maskStr,
  144. splitx: splitx,
  145. toFixed: toFixed,
  146. showMoney: showMoney,
  147. formatSeconds: formatSeconds,
  148. formatHourSeconds: formatHourSeconds,
  149. filterHalf: filterHalf,
  150. max: max,
  151. min: min,
  152. getOption: getOption,
  153. getOptionIndex: getOptionIndex,
  154. formatTimeNoSecond:formatTimeNoSecond
  155. }