util.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // utils.js
  2. export function formatTime(time) {
  3. if (typeof time !== 'number' || time < 0) {
  4. return time.toString();
  5. }
  6. const hour = Math.floor(time / 3600);
  7. time %= 3600;
  8. const minute = Math.floor(time / 60);
  9. const second = time % 60;
  10. return [hour, minute, second].map(n => n.toString().padStart(2, '0')).join(':');
  11. }
  12. export function formatLocation(longitude, latitude) {
  13. if (typeof longitude === 'string' && typeof latitude === 'string') {
  14. longitude = parseFloat(longitude);
  15. latitude = parseFloat(latitude);
  16. }
  17. return {
  18. longitude: longitude.toFixed(2).split('.'),
  19. latitude: latitude.toFixed(2).split('.')
  20. };
  21. }
  22. export const dateUtils = {
  23. UNITS: {
  24. '年': 31557600000,
  25. '月': 2629800000,
  26. '天': 86400000,
  27. '小时': 3600000,
  28. '分钟': 60000,
  29. '秒': 1000
  30. },
  31. humanize(milliseconds) {
  32. for (const key in this.UNITS) {
  33. if (milliseconds >= this.UNITS[key]) {
  34. return Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  35. }
  36. }
  37. return '刚刚';
  38. },
  39. format(dateStr) {
  40. const date = this.parse(dateStr);
  41. const diff = Date.now() - date.getTime();
  42. if (diff < this.UNITS['天']) {
  43. return this.humanize(diff);
  44. }
  45. const _format = number => (number < 10 ? `0${number}` : number);
  46. return `${date.getFullYear()}/${_format(date.getMonth() + 1)}/${_format(date.getDate())} - ${_format(date.getHours())}:${_format(date.getMinutes())}`;
  47. },
  48. parse(str) { // 将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  49. const a = str.split(/[^0-9]/);
  50. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  51. }
  52. };
  53. export function formateDates(datetime, type) {
  54. const year = datetime.getFullYear();
  55. const month = String(datetime.getMonth() + 1).padStart(2, '0');
  56. const date = String(datetime.getDate()).padStart(2, '0');
  57. const hour = String(datetime.getHours()).padStart(2, '0');
  58. const minute = String(datetime.getMinutes()).padStart(2, '0');
  59. const second = String(datetime.getSeconds()).padStart(2, '0');
  60. switch (type) {
  61. case "Y-M-D h:min:s":
  62. return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
  63. case "Y-M-D":
  64. return `${year}-${month}-${date}`;
  65. case "h:min:s":
  66. return `${hour}:${minute}:${second}`;
  67. default:
  68. return '';
  69. }
  70. }
  71. export function deteleObject(obj) {
  72. const uniques = [];
  73. const stringify = {};
  74. obj.forEach(item => {
  75. const keys = Object.keys(item).sort();
  76. const str = keys.map(key => JSON.stringify([key, item[key]])).join('');
  77. if (!stringify[str]) {
  78. uniques.push(item);
  79. stringify[str] = true;
  80. }
  81. });
  82. return uniques;
  83. }
  84. export function formateDate(datetime, type) {
  85. const year = datetime.getFullYear();
  86. const month = String(datetime.getMonth() + 1).padStart(2, '0');
  87. const date = String(datetime.getDate()).padStart(2, '0');
  88. const hour = String(datetime.getHours()).padStart(2, '0');
  89. const minute = String(datetime.getMinutes()).padStart(2, '0');
  90. const second = String(datetime.getSeconds()).padStart(2, '0');
  91. switch (type) {
  92. case "Y-M-D h:min:s":
  93. return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
  94. case "Y-M-D":
  95. return `${year}年${month}月${date}日`;
  96. case "h:min:s":
  97. return `${hour}:${minute}:${second}`;
  98. case "h":
  99. return hour;
  100. case "min":
  101. return minute;
  102. default:
  103. return '';
  104. }
  105. }
  106. export function randomNum(minNum, maxNum = null) {
  107. if (maxNum === null) {
  108. maxNum = minNum;
  109. minNum = 0;
  110. }
  111. return Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum;
  112. }
  113. export function pointInsideCircle(point, circle, r) {
  114. if (r === 0) return false;
  115. const dx = circle[0] - point[0];
  116. const dy = circle[1] - point[1];
  117. return dx * dx + dy * dy <= r * r;
  118. }
  119. export function isSameDay(timeStampA) {
  120. const dateA = new Date(timeStampA);
  121. const dateB = new Date();
  122. return dateA.setHours(0, 0, 0, 0) === dateB.setHours(0, 0, 0, 0);
  123. }
  124. export function getCurrentMonthFirst() {
  125. const date = new Date();
  126. date.setDate(1);
  127. return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
  128. }
  129. export function getCurrentMonthLast() {
  130. const date = new Date();
  131. date.setMonth(date.getMonth() + 1, 0);
  132. return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
  133. }