util.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  9. }
  10. const formatDate = date => {
  11. const year = date.getFullYear()
  12. const month = date.getMonth() + 1
  13. const day = date.getDate()
  14. return `${[year, month, day].map(formatNumber).join('-')} `
  15. }
  16. const formatNumber = n => {
  17. n = n.toString()
  18. return n[1] ? n : `0${n}`
  19. }
  20. const simleInfo = msg => {
  21. wx.showModal({
  22. title: '提示',
  23. content: msg,
  24. showCancel: false,
  25. success(res) {
  26. }
  27. })
  28. }
  29. const simleInfoWithBack = msg => {
  30. wx.showModal({
  31. title: '提示',
  32. content: msg,
  33. showCancel: false,
  34. success(res) {
  35. wx.navigateBack({})
  36. }
  37. })
  38. }
  39. function formatDateToYMD(dateString) {
  40. if (!dateString) return '';
  41. try {
  42. const date = new Date(dateString);
  43. const year = date.getFullYear();
  44. const month = String(date.getMonth() + 1).padStart(2, '0');
  45. const day = String(date.getDate()).padStart(2, '0');
  46. return `${year}年${month}月${day}日`;
  47. } catch (e) {
  48. console.error('日期格式化错误:', e);
  49. return dateString; // 返回原字符串或自定义错误提示
  50. }
  51. }
  52. function floatAdd(a, b) {
  53. var c, d, e;
  54. if(undefined==a||null==a||""==a||isNaN(a)){a=0;}
  55. if(undefined==b||null==b||""==b||isNaN(b)){b=0;}
  56. try {
  57. c = a.toString().split(".")[1].length;
  58. } catch (f) {
  59. c = 0;
  60. }
  61. try {
  62. d = b.toString().split(".")[1].length;
  63. } catch (f) {
  64. d = 0;
  65. }
  66. e = Math.pow(10, Math.max(c, d));
  67. return (floatMul(a, e) + floatMul(b, e)) / e;
  68. }
  69. function floatSub(a, b) {
  70. var c, d, e;
  71. if(undefined==a||null==a||""==a||isNaN(a)){a=0;}
  72. if(undefined==b||null==b||""==b||isNaN(b)){b=0;}
  73. try {
  74. c = a.toString().split(".")[1].length;
  75. } catch (f) {
  76. c = 0;
  77. }
  78. try {
  79. d = b.toString().split(".")[1].length;
  80. } catch (f) {
  81. d = 0;
  82. }
  83. e = Math.pow(10, Math.max(c, d));
  84. return (floatMul(a, e) - floatMul(b, e)) / e;
  85. }
  86. function floatMul(a, b) {
  87. var c = 0,
  88. d = a.toString(),
  89. e = b.toString();
  90. try {
  91. c += d.split(".")[1].length;
  92. } catch (f) {}
  93. try {
  94. c += e.split(".")[1].length;
  95. } catch (f) {}
  96. return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
  97. }
  98. module.exports = {
  99. simleInfo: simleInfo,
  100. simleInfoWithBack: simleInfoWithBack,
  101. formatNumber: formatNumber,
  102. formatTime: formatTime,
  103. formatDate: formatDate,
  104. floatMul:floatMul,
  105. floatAdd:floatAdd,
  106. floatSub:floatSub
  107. }