聚星移动端

common.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. export function getDay (day,haveHours){
  2. //day为0表示今天的日期 -1为昨天 haveHours存在表示要时分秒 haveHours:true,false
  3. var today = new Date();
  4. var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
  5. today.setTime(targetday_milliseconds); //注意,这行是关键代码
  6. var tYear = today.getFullYear();
  7. var tMonth = today.getMonth();
  8. var tDate = today.getDate();
  9. var hours = today.getHours();
  10. var minutes = today.getMinutes(); //分
  11. var seconds = today.getSeconds(); //秒
  12. tMonth = doHandleMonth(tMonth + 1);
  13. tDate = doHandleMonth(tDate);
  14. if(haveHours){
  15. if(day == 0){
  16. return {
  17. 'now':tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + (minutes.toString().length == 1 ? '0'+minutes : minutes) + ':' + (seconds.toString().length == 1 ? '0'+seconds : seconds),
  18. 'zero':tYear+"-"+tMonth+"-"+tDate+' ' + '00:00:00'
  19. }
  20. }else{
  21. return tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds
  22. }
  23. }else{
  24. return tYear+"-"+tMonth+"-"+tDate
  25. }
  26. function doHandleMonth(month){
  27. var m = month;
  28. if(month.toString().length == 1){
  29. m = "0" + month;
  30. }
  31. return m;
  32. }
  33. }
  34. export function NumberHandle (value,numberDigit) {//数值小数点处理 ①5997;②8.1w;③2489kw;④4.2亿 且保留一位小数
  35. if(typeof parseFloat(value) === 'number' && !isNaN(value)){
  36. //判断是否是数值类型
  37. if((value >= 10000 && value < 100000000) || (value <= -10000 && value > -100000000)){
  38. if(hasDot(value/10000000,numberDigit)==10){
  39. return '1亿'
  40. }else{
  41. return hasDot(value/10000,numberDigit) + 'w'
  42. }
  43. }else if(value >= 100000000 || value <= -100000000){
  44. return hasDot(value/100000000,numberDigit) + '亿'
  45. }else{
  46. if(value==undefined){
  47. return '-'
  48. }else{
  49. return hasDot(value,numberDigit)
  50. }
  51. }
  52. }else{
  53. if(value==undefined){
  54. return '-'
  55. }else{
  56. return value
  57. }
  58. }
  59. }
  60. function hasDot(num,numberDigit){
  61. //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
  62. var digit = 1;
  63. if(numberDigit){
  64. digit = numberDigit
  65. }
  66. return formatNum((num + '').indexOf('.') != -1 ? parseFloat(Number(num).toFixed(digit)) : num);
  67. }
  68. //复制
  69. export function copy(data,cb) {
  70. var copyBox = document.createElement("p");
  71. copyBox.innerText = data;
  72. //把创建后的子元素追加到父级元素中
  73. document.body.appendChild(copyBox)
  74. if (document.body.createTextRange) {
  75. //createTextRange是用在IE中的
  76. var range = document.body.createTextRange();
  77. range.moveToElementText(copyBox);
  78. range.select();
  79. } else if (window.getSelection) {
  80. var selection = window.getSelection();
  81. var range = document.createRange();
  82. range.selectNodeContents(copyBox);
  83. selection.removeAllRanges();
  84. selection.addRange(range);
  85. document.execCommand("Copy")
  86. }
  87. copyBox.remove();
  88. cb?cb():''
  89. }
  90. // 检测是否全是中文
  91. export function isChn(str){
  92. var reg = /^[\u4E00-\u9FA5]+$/;
  93. if(!reg.test(str)){
  94. // alert("不是中文");
  95. return false;
  96. }
  97. //alert("中文");
  98. return true;
  99. }
  100. export function formatNum (str){
  101. if (typeof parseFloat(str) === 'number' && !isNaN(str) && str != null) {
  102. //判断是否是数值类型
  103. var newStr = "";
  104. var count = 0;
  105. str = Number(str).toFixed(2)
  106. str = parseFloat(str)
  107. str = str.toString();
  108. if (str.indexOf(".") == -1) {
  109. for (var i = str.length - 1; i >= 0; i--) {
  110. if (count % 3 == 0 && count != 0 && str.charAt(i) != '-') {
  111. newStr = str.charAt(i) + "," + newStr;
  112. } else {
  113. newStr = str.charAt(i) + newStr;
  114. }
  115. count++;
  116. }
  117. str = newStr; //自动补小数点后两位
  118. } else {
  119. for (var i = str.indexOf(".") - 1; i >= 0; i--) {
  120. if (count % 3 == 0 && count != 0 && str.charAt(i) != '-') {
  121. newStr = str.charAt(i) + "," + newStr;
  122. } else {
  123. newStr = str.charAt(i) + newStr; //逐个字符相接起来
  124. }
  125. count++;
  126. }
  127. str = newStr + str.substring(str.indexOf("."), str.length);
  128. }
  129. }
  130. return str
  131. }
  132. //秒转化为时
  133. export function formateHours(endTime){
  134. if(endTime){
  135. let secondTime = parseInt(endTime)//将传入的秒的值转化为Number
  136. let h =0// 初始化小时
  137. let result=''
  138. h = parseFloat((secondTime/3600).toFixed(1))
  139. result=h+"h"
  140. return result
  141. }else{
  142. return "0h"
  143. }
  144. }