tools.sjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. var strToInt = function(n) {
  13. return parseInt(n)
  14. }
  15. var minuteToHour = function(minute) {
  16. // minute = Math.ceil(minute/60)
  17. var hour = Math.floor(minute / 60)
  18. var remainMinute = minute - hour * 60
  19. var minutehour = ""
  20. if (hour != 0) {
  21. minutehour = remainMinute != 0 ? (hour + "小时" + remainMinute + "分钟") : (hour + "小时")
  22. } else {
  23. minutehour = remainMinute != 0 ? (remainMinute + "分钟") : (hour + "小时")
  24. }
  25. return minutehour
  26. //return remainMinute !=0 ? (hour+"小时"+remainMinute+"分钟"):(hour+"小时")
  27. }
  28. /**
  29. * 时间戳格式化时间
  30. */
  31. var formatTime = function(datetime) {
  32. if (datetime == 0 ||!datetime) {
  33. return ""
  34. }
  35. var date = getDate(datetime * 1000);
  36. var year = date.getFullYear()
  37. var month = date.getMonth() + 1
  38. var day = date.getDate()
  39. var hour = date.getHours()
  40. var minute = date.getMinutes()
  41. var second = date.getSeconds()
  42. return year + '-' + formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' +
  43. formatNumber(minute) + ':' + formatNumber(second);
  44. }
  45. // 时间戳格式化 返回日期
  46. var formatGetDate = function(datetime) {
  47. var date = getDate(datetime * 1000);
  48. var year = date.getFullYear()
  49. var month = date.getMonth() + 1
  50. var day = date.getDate()
  51. return year + '-' + formatNumber(month) + '-' + formatNumber(day);
  52. }
  53. // 时间戳格式化 返回x月x日 x:x
  54. var formatTimeDate = function(datetime) {
  55. if (datetime == 0) {
  56. return ""
  57. }
  58. var date = getDate(datetime * 1000);
  59. var month = date.getMonth() + 1
  60. var day = date.getDate()
  61. var hour = date.getHours()
  62. var minute = date.getMinutes()
  63. var second = date.getSeconds()
  64. return formatNumber(month) + '月' + formatNumber(day) + '日 ' + formatNumber(hour) + ':' + formatNumber(minute);
  65. }
  66. // 时间戳格式化 返回x-x x:x
  67. var formatTimeSecond = function(datetime) {
  68. if (datetime == 0) {
  69. return ""
  70. }
  71. var date = getDate(datetime * 1000);
  72. var month = date.getMonth() + 1
  73. var day = date.getDate()
  74. var hour = date.getHours()
  75. var minute = date.getMinutes()
  76. var second = date.getSeconds()
  77. return formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' + formatNumber(minute);
  78. }
  79. var formatDateSplit = function(dateString) {
  80. const parts = dateString.split('-');
  81. return parts[0] + parts[1] + parts[2];
  82. }
  83. var daysBetweenTimestamps = function(timestamp1, timestamp2) {
  84. const date1 = formatDateSplit(formatGetDate(timestamp1));
  85. const date2 = formatDateSplit(formatGetDate(timestamp2));
  86. const diff = date2 - date1;
  87. return diff;
  88. }
  89. /**
  90. * 隐藏字符串 多余字符使用*替代
  91. */
  92. var maskStr = function(str, replace, len) {
  93. if (typeof str != 'string')
  94. return '';
  95. if (str.length < len) return str;
  96. return str.substring(0, len) + replace + replace + replace;
  97. }
  98. /**
  99. * 将字符串分割成数组
  100. */
  101. var splitx = function(str, splitstr) {
  102. if (typeof str != 'string')
  103. return [];
  104. return str.split(splitstr);
  105. }
  106. var toFix = function(value) {
  107. //return value.toFixed(2)
  108. return parseFloat(parseFloat(value).toFixed(2)) //此处2为保留两位小数
  109. }
  110. var toFixLength = function(value) {
  111. return parseFloat(parseFloat(value).toFixed(2)).toString().length //此处2为保留两位小数
  112. }
  113. var toFixThree = function(value) {
  114. return parseFloat(parseFloat(value).toFixed(3)) //此处3为保留两位小数
  115. }
  116. var toFixCeli = function(value) {
  117. return parseFloat(parseFloat(Math.ceil(value * 100) / 100).toFixed(2))
  118. //return parseFloat(parseFloat(value).toFixed(2))//此处2为保留两位小数
  119. }
  120. var formatToday = function() {
  121. var date = getDate()
  122. var year = date.getFullYear()
  123. var month = date.getMonth() + 1
  124. var day = date.getDate()
  125. var wday = date.getDay()
  126. var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  127. return [formatNumber(month) + '月' + formatNumber(day) + '日', weekday[wday]];
  128. }
  129. //格式化时间 12:00
  130. var timestampToHHMM = function(timestamp) {
  131. var date = getDate(timestamp);
  132. var hours = date.getHours().toString().padStart(2, '0');
  133. var minutes = date.getMinutes().toString().padStart(2, '0');
  134. return hours + ':' + minutes;
  135. }
  136. var formatDistance = function(distanceMeters) {
  137. // 判断距离是否超过1000米
  138. // let distanceMeters=Number(_distanceMeters)
  139. if (distanceMeters >= 1000) {
  140. // 如果超过1000米,则转换为千米并返回
  141. return (distanceMeters / 1000).toFixed(2) + 'km';
  142. } else {
  143. // 否则直接返回米
  144. return distanceMeters.toFixed(0) + 'm';
  145. }
  146. }
  147. var formatWeight = function(weightMeters) {
  148. if (weightMeters >= 1000) {
  149. return (weightMeters / 1000).toFixed(2) + 'kg';
  150. } else {
  151. return weightMeters.toFixed(0) + 'g';
  152. }
  153. }
  154. var countToDay = function(count, unit) {
  155. var data = '';
  156. switch (unit - 0) {
  157. case 1: //日
  158. data = count
  159. break;
  160. case 2: //月
  161. data = count * 30
  162. break;
  163. case 4: //时
  164. data = count
  165. break;
  166. case 6: //周
  167. data = count * 7
  168. break;
  169. case 7: //季
  170. data = count * 90
  171. break;
  172. }
  173. return data;
  174. }
  175. export default {
  176. numToArray: numToArray,
  177. formatTime: formatTime,
  178. formatGetDate: formatGetDate,
  179. formatTimeDate: formatTimeDate,
  180. formatTimeSecond: formatTimeSecond,
  181. daysBetweenTimestamps: daysBetweenTimestamps,
  182. formatDateSplit: formatDateSplit,
  183. maskStr: maskStr,
  184. splitx: splitx,
  185. toFix: toFix,
  186. toFixLength: toFixLength,
  187. toFixCeli: toFixCeli,
  188. formatToday: formatToday,
  189. minuteToHour: minuteToHour,
  190. strToInt: strToInt,
  191. timestampToHHMM: timestampToHHMM,
  192. formatDistance: formatDistance,
  193. formatWeight: formatWeight,
  194. toFixThree: toFixThree,
  195. countToDay: countToDay
  196. }