public.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. export const getTime = (time) => {
  2. const date = new Date(time);
  3. let Y = date.getFullYear() + '-';
  4. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  5. let D = date.getDate() + ' ';
  6. return (Y + M + D)
  7. };
  8. const getUnix = () => {
  9. const date = new Date();
  10. return date.getTime();
  11. };
  12. const getTodayUnix = () => {
  13. const date = new Date();
  14. date.setHours(0);
  15. date.setMinutes(0);
  16. date.setSeconds(0);
  17. date.setMilliseconds(0);
  18. return date.getTime();
  19. };
  20. const getLastDate = (time) => {
  21. const date = new Date(time);
  22. const month =
  23. date.getMonth() + 1 < 10
  24. ? "0" + (date.getMonth() + 1)
  25. : date.getMonth() + 1;
  26. const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  27. return date.getFullYear() + "-" + month + "-" + day;
  28. };
  29. export const getFormatTime = (timestamp) => {
  30. const now = getUnix(); // 当前时间戳
  31. const today = getTodayUnix(); // 今天0点的时间戳
  32. const timer = (now - timestamp) / 1000; // 转换为秒级时间戳
  33. let tip = '';
  34. if (timer <= 0) {
  35. tip = "刚刚";
  36. } else if (Math.floor(timer / 60) <= 0) {
  37. tip = "刚刚";
  38. } else if (timer < 3600) {
  39. tip = Math.floor(timer / 60) + "分钟前";
  40. } else if (timer >= 3600 && timestamp - today >= 0) {
  41. tip = Math.floor(timer / 3600) + "小时前";
  42. } else if (timer / 86400 <= 31) {
  43. tip = Math.ceil(timer / 86400) + "天前";
  44. } else {
  45. tip = getLastDate(timestamp);
  46. }
  47. return tip;
  48. };
  49. export const getrand = (m, n) => {
  50. return Math.floor(Math.random() * (n - m + 1)) + m;
  51. };