index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * 数字保留两位小数点
  3. * @param {number} num
  4. * @return {string}
  5. */
  6. export function priceFilter(num) {
  7. if(!num) return '0.00';
  8. num = Number(num);
  9. return num.toFixed(2);
  10. }
  11. /**
  12. * 手机号加密
  13. * @param {string} val
  14. * @return {string}
  15. */
  16. export function phoneFilter(val) {
  17. if (!val) return '';
  18. return val.slice(0, 3) + '****' + val.slice(7);
  19. }
  20. /**
  21. * 日期转 年月日格式 YY-mm-dd
  22. * @param {string} date
  23. * @return {string}
  24. */
  25. export function dateToYYmmdd(date) {
  26. if(!date) return '';
  27. return date.slice(0, 10);
  28. }
  29. /**
  30. * 日期转 年月日格式 YY.mm.dd
  31. * @param {string} date
  32. * @return {string}
  33. */
  34. export function dateToYYmmdd2(date) {
  35. if(!date) return '';
  36. let newDate = date.slice(0, 10);
  37. newDate = newDate.replace(/-/g, '.');
  38. return newDate;
  39. }
  40. /**
  41. * 日期转 月日格式
  42. * @param {string} date
  43. * @return {string}
  44. */
  45. export function dateTommdd(date) {
  46. if(!date) return '';
  47. return date.slice(5, 10);
  48. }
  49. /**
  50. * 日期转 时分秒格式
  51. * @param {string} date
  52. * @return {string}
  53. */
  54. export function dateToHHmmss(date) {
  55. if(!date) return '';
  56. return date.slice(11, 19);
  57. }
  58. export function priceFilter2(val) {
  59. if(val === 0) return '面议';
  60. if(!val) return '-';
  61. val = Number(val);
  62. return `¥${val.toFixed(2)}`;
  63. }
  64. /**
  65. * @param {number} time
  66. * @returns {string}
  67. */
  68. export function timeFilter(date) {
  69. date = new Date(date);
  70. let time = date.getTime();
  71. if (('' + time).length === 10) {
  72. time = parseInt(time) * 1000
  73. } else {
  74. time = +time
  75. }
  76. const d = new Date(time)
  77. const now = Date.now()
  78. const diff = (now - d) / 1000
  79. if (diff < 30) {
  80. return '刚刚'
  81. } else if (diff < 3600) {
  82. return Math.ceil(diff / 60) + '分钟前'
  83. } else if (diff < 3600 * 24) {
  84. return Math.ceil(diff / 3600) + '小时前'
  85. } else if (diff >= 3600 * 24) {
  86. return Math.ceil(diff / 3600 / 24) + '天前'
  87. }
  88. }