wjw.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //显示日期
  2. //时间计算
  3. export function showtime(time) {
  4. let date =
  5. typeof time === "number" ?
  6. new Date(time) :
  7. new Date((time || "").replace(/-/g, "/"));
  8. let diff = (new Date().getTime() - date.getTime()) / 1000;
  9. let dayDiff = Math.floor(diff / 86400);
  10. let year = new Date().getFullYear()
  11. let nowyear = new Date(time).getFullYear()
  12. let isValidDate =
  13. Object.prototype.toString.call(date) === "[object Date]" &&
  14. !isNaN(date.getTime());
  15. if (!isValidDate) {
  16. window.console.error("不是有效日期格式");
  17. }
  18. // const formatDate = function(date) {
  19. // let today = new Date(date);
  20. // let year = today.getFullYear();
  21. // let month = ("0" + (today.getMonth() + 1)).slice(-2);
  22. // let day = ("0" + today.getDate()).slice(-2);
  23. // let hour = today.getHours();
  24. // let minute = today.getMinutes();
  25. // let second = today.getSeconds();
  26. // return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  27. // };
  28. // if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) {
  29. // return formatDate(date);
  30. // }
  31. return (
  32. (dayDiff === 0 &&
  33. ((diff < 60 && "刚刚") ||
  34. (diff < 120 && "1分钟前") ||
  35. (diff < 3600 && Math.floor(diff / 60) + "分钟前") ||
  36. (diff < 7200 && "1小时前") ||
  37. (diff < 86400 && Math.floor(diff / 3600) + "小时前"))) ||
  38. (dayDiff === 1 && "昨天") ||
  39. (dayDiff < 7 && dayDiff + "天前") ||
  40. // (dayDiff < 31 && Math.ceil(dayDiff / 7) + "周前")||
  41. (year == nowyear && time.slice(5)) ||
  42. (year > nowyear && time)
  43. );
  44. }
  45. //获取当前周数
  46. export function getWeekInYear(date) {
  47. // console.log(date)
  48. //判断该星期是否跨年,如果跨年就是第一周
  49. let weekStartDate = getWeekStartByDate(date) //一周开始时间
  50. // console.log(weekStartDate,'weekStartDate')
  51. let endDate = getDateFromDay(weekStartDate, 6) //一周结束时间
  52. // console.log(endDate,'endDate')
  53. if (weekStartDate.getFullYear() != endDate.getFullYear()) {
  54. // console.log(endDate.getFullYear() + '年第1周')
  55. let num = 1
  56. return num
  57. }
  58. let d1 = new Date(date)
  59. let d2 = new Date(date)
  60. d2.setMonth(0)
  61. d2.setDate(1)
  62. d2 = getWeekStartByDate(d2)
  63. // console.log(d2,'d2')
  64. let rq = d1 - d2
  65. let days = Math.ceil(rq / (24 * 60 * 60 * 1000)) + 1
  66. let num = Math.ceil(days / 7)
  67. // console.log(endDate.getFullYear() + '年第' + num + '周')
  68. return num
  69. }
  70. function getWeekStartByDate(date) {
  71. let day = new Date(date).getDay() == 0 ? 6 : new Date(date).getDay() - 1
  72. return getDateFromDay(date, -day)
  73. }
  74. function getDateFromDay(dayDate, day) {
  75. let date = new Date()
  76. date.setTime(new Date(dayDate).getTime() + day * 24 * 60 * 60 * 1000)
  77. return date
  78. }
  79. //获取本周一周日期
  80. export function getNowWeekList(data) {
  81. let now = new Date(data); // 当前日期
  82. let nowTime = now.getTime()
  83. let nowDayOfWeek = now.getDay() == 0 ? 6 : now.getDay() - 1; // 今天本周的第几天
  84. let dayList = []
  85. let startTime = uni.$u.timeFormat((nowTime - nowDayOfWeek * 86400000), 'yyyy-mm-dd');
  86. let start = new Date(startTime).getTime()
  87. dayList.push(startTime)
  88. for (let i = 1; i < 7; i++) {
  89. dayList.push(uni.$u.timeFormat((start + i * 86400000), 'yyyy-mm-dd'))
  90. if (i == 6) {
  91. return dayList
  92. }
  93. }
  94. }
  95. //获取树形结构
  96. export function handleTree(data, id, parentId, children) {
  97. let config = {
  98. id: id || 'id',
  99. parentId: parentId || 'parentId',
  100. childrenList: children || 'children'
  101. };
  102. var childrenListMap = {};
  103. var nodeIds = {};
  104. var tree = [];
  105. for (let d of data) {
  106. let parentId = d[config.parentId];
  107. if (childrenListMap[parentId] == null) {
  108. childrenListMap[parentId] = [];
  109. }
  110. nodeIds[d[config.id]] = d;
  111. childrenListMap[parentId].push(d);
  112. }
  113. for (let d of data) {
  114. let parentId = d[config.parentId];
  115. if (nodeIds[parentId] == null) {
  116. tree.push(d);
  117. }
  118. }
  119. for (let t of tree) {
  120. adaptToChildrenList(t);
  121. }
  122. function adaptToChildrenList(o) {
  123. if (childrenListMap[o[config.id]] !== null) {
  124. o[config.childrenList] = childrenListMap[o[config.id]];
  125. }
  126. if (o[config.childrenList]) {
  127. for (let c of o[config.childrenList]) {
  128. adaptToChildrenList(c);
  129. }
  130. }
  131. }
  132. return tree;
  133. }