util.wxs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. function formatData(val) {
  2. return getDate(val * 1000).toLocaleString()
  3. }
  4. function formatArr(val) {
  5. var special_confArr = val.toString(2).split('').slice(0, val.toString(2).length - 1).reverse()
  6. return getInital(special_confArr)
  7. }
  8. function getDateDiff(now, dateTime) {
  9. var result = '';
  10. var minute = 1000 * 60;
  11. var diffValue = now - dateTime;
  12. if (diffValue < 0) {
  13. return result = "刚刚";
  14. }
  15. var minEnd = diffValue / minute;
  16. var date = getDate(dateTime);
  17. var nowdate = getDate(now);
  18. var y = date.getFullYear();
  19. var ny = nowdate.getFullYear();
  20. var m = formatNumber(date.getMonth() + 1);
  21. var nm = formatNumber(nowdate.getMonth() + 1);
  22. var w = date.getDay();
  23. var d = formatNumber(date.getDate());
  24. var nd = formatNumber(nowdate.getDate());
  25. var h = formatNumber(date.getHours());
  26. var i = formatNumber(date.getMinutes());
  27. var dayEnd;
  28. var yearEnd = ny - y;
  29. var year;
  30. if (nm - m == 1) {
  31. switch (parseInt(m)) {
  32. case 01:
  33. dayEnd = parseInt(nd) + 31 - parseInt(d);
  34. break;
  35. case 02:
  36. if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
  37. year = 29;
  38. } else {
  39. year = 28;
  40. }
  41. dayEnd = parseInt(nd) + year - parseInt(d);
  42. break;
  43. case 03:
  44. dayEnd = parseInt(nd) + 31 - parseInt(d);
  45. break;
  46. case 04:
  47. dayEnd = parseInt(nd) + 30 - parseInt(d);
  48. break;
  49. case 05:
  50. dayEnd = parseInt(nd) + 31 - parseInt(d);
  51. break;
  52. case 06:
  53. dayEnd = parseInt(nd) + 30 - parseInt(d);
  54. break;
  55. case 07:
  56. dayEnd = parseInt(nd) + 31 - parseInt(d);
  57. break;
  58. case 08:
  59. dayEnd = parseInt(nd) + 31 - parseInt(d);
  60. break;
  61. case 09:
  62. dayEnd = parseInt(nd) + 30 - parseInt(d);
  63. break;
  64. case 10:
  65. dayEnd = parseInt(nd) + 31 - parseInt(d);
  66. break;
  67. case 11:
  68. dayEnd = parseInt(nd) + 30 - parseInt(d);
  69. break;
  70. default:
  71. break;
  72. }
  73. } else if (nm - m == 0) {
  74. dayEnd = nd - d;
  75. } else {
  76. dayEnd = 7
  77. }
  78. if (yearEnd >= 1) {
  79. result = y + "年" + m + "月" + d + "日";
  80. } else if (dayEnd >= 7 && yearEnd < 1) {
  81. result = m + "月" + d + "日";
  82. } else if (dayEnd >= 2 && dayEnd < 7) {
  83. result = "" + getWeekDay(w);
  84. } else if (dayEnd >= 1 && dayEnd < 2) {
  85. result = "" + "昨天" + "" + h + ':' + i;
  86. } else if (minEnd >= 60 && dayEnd < 1) {
  87. result = "" + h + ':' + i;
  88. } else if (minEnd >= 1 && minEnd < 60) {
  89. result = "" + parseInt(minEnd) + "分钟前";
  90. } else {
  91. result = "刚刚";
  92. }
  93. return result;
  94. };
  95. function formatNumber(n) {
  96. n = n.toString()
  97. return n[1] ? n : '0' + n
  98. }
  99. function getWeekDay(index) {
  100. var show_day = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  101. return show_day[index]
  102. }
  103. function getInital(arr) {
  104. var result = []
  105. for (var i = 0; i < arr.length; i++) {
  106. if (arr[i] == 1) {
  107. result.push(i + 1 + '')
  108. }
  109. }
  110. return result
  111. }
  112. function transitionTime(val, date = "") {
  113. var time = getDate(val * 1000).toLocaleString()
  114. var reg = getRegExp(':', 'g');
  115. var timearr = time.replace(" ", ":").replace(reg, "/").split("/");
  116. var timestr = date ? timearr[0] + "年" : timearr[0] + "年" + timearr[1] + "月" + timearr[2] + "日";
  117. return timestr
  118. }
  119. module.exports = {
  120. formatData: formatData,
  121. formatArr: formatArr,
  122. transitionTime: transitionTime,
  123. getDateDiff: getDateDiff
  124. }