myfun.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // 返回两个日期之间的月份
  2. export function getDiffDate(minDate, maxDate) {
  3. let startDate = new Date(minDate.replace(/-/g, '/'));
  4. let endDate = new Date(maxDate.replace(/-/g, '/'));
  5. let months = [];
  6. //把时间的天数都设置成当前月第一天
  7. startDate.setDate(1)
  8. endDate.setDate(1)
  9. // new Date(yyyy-MM-dd) 不知为何有时候小时是 08 有时候是00
  10. endDate.setHours(0)
  11. startDate.setHours(0)
  12. while (endDate.getTime() >= startDate.getTime()) {
  13. let year = startDate.getFullYear();
  14. let month = startDate.getMonth() + 1;
  15. //加一个月
  16. startDate.setMonth(month)
  17. if (month.toString().length == 1) {
  18. month = "0" + month;
  19. }
  20. months.push(year + "-" + month)
  21. }
  22. return months;
  23. }
  24. // 时间格式化
  25. export function times(date, ym) {
  26. date = new Date(date.replace(/-/g, '/'));
  27. var y = date.getFullYear();
  28. var m = date.getMonth() + 1;
  29. m = m < 10 ? "0" + m : m;
  30. var d = date.getDate();
  31. d = d < 10 ? "0" + d : d;
  32. var h = date.getHours();
  33. h = h < 10 ? "0" + h : h;
  34. var minute = date.getMinutes();
  35. minute = minute < 10 ? "0" + minute : minute;
  36. var second = date.getSeconds();
  37. second = second < 10 ? "0" + second : second;
  38. if (ym == "ym") {
  39. return y + "-" + m;
  40. }
  41. else if (ym == "ymd") {
  42. return y + "-" + m + "-" + d;
  43. }
  44. else if (ym == "ymds") {
  45. return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
  46. }
  47. }
  48. // 将rgb颜色转成hex
  49. export function RtoH(color) {
  50. var rgb = color.split(',');
  51. var r = parseInt(rgb[0].split('(')[1]);
  52. var g = parseInt(rgb[1]);
  53. var b = parseInt(rgb[2].split(')')[0]);
  54. var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  55. return hex;
  56. }
  57. // 将hex颜色转成rgb
  58. export function HtoR(hex, opacity) {
  59. var RGBA = "rgba(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + "," + opacity + ")";
  60. // return { red: parseInt("0x" + hex.slice(1, 3)), green: parseInt("0x" + hex.slice(3, 5)), blue: parseInt("0x" + hex.slice(5, 7)), rgba: RGBA }
  61. return RGBA;
  62. }
  63. // 189 **** 0222
  64. export function phoneNumA(value) {
  65. if (value && value.length > 8) {
  66. return `${value.substring(0, 3)} ${"*".repeat(value.length - 7).replace(/(.{4})/g, "$1 ")}${value.length % 4 ? " " : ""}${value.slice(-4)}`;
  67. }
  68. return value;
  69. }
  70. // goto
  71. export function goto(url, json) {
  72. let params = "";
  73. if (json) {
  74. params = [];
  75. let da = Object.keys(json).map(function (key) {
  76. if (json[key]) return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
  77. });
  78. da.forEach(e => {
  79. if (e) params.push(e)
  80. });
  81. params = params.join("&")
  82. }
  83. uni.navigateTo({
  84. url: url + '?' + params
  85. });
  86. }
  87. // tidyTpye
  88. export function tidyTpye(va) {
  89. switch (va) {
  90. case '1': return "零售专区";
  91. case '2': return "批发专区";
  92. case '3': return "精品优选";
  93. case '4': return "今日值得买";
  94. case '5': return "茶宝兑换";
  95. case '6': return "天天捡漏";
  96. default: return ""
  97. }
  98. }