index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * 时间戳转换成时间
  3. * @param {*} time
  4. */
  5. const formDate = (time, formate = "yyyy-mm-dd hh:mm:ss") => {
  6. var padDate = function(va) {
  7. va = va < 10 ? "0" + va : va;
  8. return va;
  9. };
  10. if (time) {
  11. var value = new Date(time * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  12. // var value = new Date(parseFloat(time))
  13. var year = value.getFullYear();
  14. var month = padDate(value.getMonth() + 1);
  15. var day = padDate(value.getDate());
  16. var hour = padDate(value.getHours());
  17. var minutes = padDate(value.getMinutes());
  18. var seconds = padDate(value.getSeconds());
  19. let res = "";
  20. switch (formate) {
  21. case "mm-dd": {
  22. res = month + "-" + day;
  23. break;
  24. }
  25. case "yyyy-mm-dd": {
  26. res = year + "-" + month + "-" + day;
  27. break;
  28. }
  29. case "yyyy-mm": {
  30. res = year + "-" + month;
  31. break;
  32. }
  33. case "mm月dd日": {
  34. res = month + "月" + day + "日";
  35. break;
  36. }
  37. case "yyyy年mm月dd日": {
  38. res = year + "年" + month + "月" + day + "日";
  39. break;
  40. }
  41. case "yyyy年mm月": {
  42. res = year + "年" + month + "月";
  43. break;
  44. }
  45. case "hh:mm": {
  46. res = hour + ":" + minutes;
  47. break;
  48. }
  49. case "yyyy-mm-dd hh:mm": {
  50. res = year + "-" + month + "-" + day + " " + hour + ":" + minutes;
  51. break;
  52. }
  53. case "yyyy.mm.dd":
  54. res = year + "." + month + "." + day;
  55. break;
  56. case "yyyy-mm-dd hh:mm:ss":
  57. default: {
  58. res =
  59. year +
  60. "-" +
  61. month +
  62. "-" +
  63. day +
  64. " " +
  65. hour +
  66. ":" +
  67. minutes +
  68. ":" +
  69. seconds;
  70. break;
  71. }
  72. }
  73. return res;
  74. }
  75. return "--";
  76. };
  77. const formDateStr = (time) => {
  78. var padDate = function(va) {
  79. va = va < 10 ? "0" + va : va;
  80. return va;
  81. };
  82. if (time) {
  83. var value = new Date(parseFloat(time));
  84. var year = value.getFullYear();
  85. var month = padDate(value.getMonth() + 1);
  86. var day = value.getDate();
  87. var hour = padDate(value.getHours());
  88. var minutes = padDate(value.getMinutes());
  89. var seconds = padDate(value.getSeconds());
  90. let res = "";
  91. if (day == new Date().getDate()) {
  92. res = "今天" + hour + ":" + minutes;
  93. return res;
  94. }
  95. if (year == new Date().getFullYear()) {
  96. res = month + "-" + padDate(day) + " " + hour + ":" + minutes;
  97. return res;
  98. }
  99. res = year + "-" + month + "-" + padDate(day);
  100. return res;
  101. }
  102. return "--";
  103. };
  104. const formatRichText = (html) => {
  105. //控制小程序中图片大小
  106. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  107. match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, "");
  108. match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, "");
  109. match = match
  110. .replace(/height="[^"]+"/gi, "")
  111. .replace(/height='[^']+'/gi, "");
  112. return match;
  113. });
  114. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  115. match = match
  116. .replace(/width:[^;]+;/gi, "max-width:100%;")
  117. .replace(/width:[^;]+;/gi, "max-width:100%;");
  118. return match;
  119. });
  120. newContent = newContent.replace(/<br[^>]*\/>/gi, "");
  121. newContent = newContent.replace(
  122. /\<img/gi,
  123. '<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"'
  124. );
  125. return newContent;
  126. };
  127. // 金额格式化
  128. const formatPrice = (price) => {
  129. price = (price || 0).toLocaleString(
  130. "zh-CN",
  131. (undefined, {
  132. minimumFractionDigits: 2,
  133. maximumFractionDigits: 2,
  134. })
  135. );
  136. return price;
  137. }
  138. export default {
  139. formDate,
  140. formDateStr,
  141. formatRichText,
  142. formatPrice
  143. };