utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // 获取任意时间
  2. export const getDate = (date, AddDayCount = 0) => {
  3. if (!date) {
  4. date = new Date();
  5. }
  6. if (typeof date !== 'object') {
  7. date = date.replace(/-/g, '/');
  8. }
  9. const dd = new Date(date);
  10. dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
  11. const y = dd.getFullYear();
  12. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期,不足10补0
  13. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号,不足10补0
  14. const dayStrList = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  15. const HMS = `${dd.getHours() < 10 ? '0' + dd.getHours() : dd.getHours()}${dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd.getMinutes()}${
  16. dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds()
  17. }`;
  18. return {
  19. fullDate: y + '-' + m + '-' + d,
  20. year: y,
  21. month: m,
  22. date: d,
  23. day: dd.getDay(),
  24. dayStr: dayStrList[dd.getDay()],
  25. rStr: `${m}月${d}日`,
  26. HMS
  27. };
  28. };
  29. // tabBar 页面路径(与 pages.json 一致),跳转这些页面须用 switchTab,不能用 redirectTo/navigateTo
  30. const TABBAR_PATHS = ['/pages/index/index', '/pages/numberOfDiners/index', '/pages/personal/index'];
  31. // 通用跳转方法
  32. export function go(url, mode = 'navigateTo') {
  33. if (url == -1) {
  34. let pages = getCurrentPages(); //获取页面
  35. let beforePage = pages[pages.length - 2]; //上个页面
  36. if (beforePage) {
  37. uni.navigateBack();
  38. } else {
  39. uni.switchTab({ url: '/pages/index/index' });
  40. }
  41. } else {
  42. const path = typeof url === 'string' ? url.split('?')[0] : '';
  43. const isTabBar = TABBAR_PATHS.some((p) => path === p || path.endsWith(p));
  44. const actualMode = isTabBar ? 'switchTab' : mode;
  45. uni[actualMode]({ url });
  46. }
  47. }
  48. export function sToHs(s) {
  49. //计算分钟
  50. //算法:将秒数除以60,然后下舍入,既得到分钟数
  51. let h;
  52. h = Math.floor(s / 60);
  53. //计算秒
  54. //算法:取得秒%60的余数,既得到秒数
  55. s = s % 60;
  56. //将变量转换为字符串
  57. h += '';
  58. s += '';
  59. //如果只有一位数,前面增加一个0
  60. h = h.length === 1 ? '0' + h : h;
  61. s = Math.floor(s * 100) / 100;
  62. s = s.length === 1 ? '0' + s : s;
  63. if (parseInt(s) < 10) s = '0' + s;
  64. return '00:' + h + ':' + s;
  65. }
  66. // 将时间格式'00:00:00.00' 转成毫秒
  67. export function timeToMilliseconds(timeString) {
  68. // 拆分时间字符串
  69. const [hours, minutes, secondsWithMs] = timeString.split(':');
  70. const [seconds, milliseconds] = secondsWithMs.split('.');
  71. // 转换为秒数
  72. const totalMilliseconds =
  73. parseInt(hours) * 3600 * 1000 + // 小时转毫秒
  74. parseInt(minutes) * 60 * 1000 + // 分钟转毫秒
  75. parseFloat(`${seconds}.${milliseconds}`) * 1000; // 秒和毫秒转毫秒
  76. return totalMilliseconds;
  77. }
  78. // 将毫秒转长度
  79. export function getTrackLength(str) {
  80. const milliseconds = timeToMilliseconds(str);
  81. return Math.round(milliseconds / 12.5);
  82. }
  83. export default {
  84. getDate,
  85. go,
  86. sToHs,
  87. timeToMilliseconds,
  88. getTrackLength
  89. };