utils.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const getDate = (date, AddDayCount = 0) => {
  4. if (!date) {
  5. date = /* @__PURE__ */ new Date();
  6. }
  7. if (typeof date !== "object") {
  8. date = date.replace(/-/g, "/");
  9. }
  10. const dd = new Date(date);
  11. dd.setDate(dd.getDate() + AddDayCount);
  12. const y = dd.getFullYear();
  13. const m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
  14. const d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  15. const dayStrList = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  16. const HMS = `${dd.getHours() < 10 ? "0" + dd.getHours() : dd.getHours()}${dd.getMinutes() < 10 ? "0" + dd.getMinutes() : dd.getMinutes()}${dd.getSeconds() < 10 ? "0" + dd.getSeconds() : dd.getSeconds()}`;
  17. return {
  18. fullDate: y + "-" + m + "-" + d,
  19. year: y,
  20. month: m,
  21. date: d,
  22. day: dd.getDay(),
  23. dayStr: dayStrList[dd.getDay()],
  24. rStr: `${m}月${d}日`,
  25. HMS
  26. };
  27. };
  28. function go(url, mode = "navigateTo") {
  29. if (url == -1) {
  30. let pages = getCurrentPages();
  31. let beforePage = pages[pages.length - 2];
  32. if (beforePage) {
  33. common_vendor.index.navigateBack();
  34. } else {
  35. common_vendor.index.reLaunch({
  36. url: "/pages/index/index"
  37. });
  38. }
  39. } else {
  40. common_vendor.index[mode]({
  41. url
  42. });
  43. }
  44. }
  45. function sToHs(s) {
  46. let h;
  47. h = Math.floor(s / 60);
  48. s = s % 60;
  49. h += "";
  50. s += "";
  51. h = h.length === 1 ? "0" + h : h;
  52. s = Math.floor(s * 100) / 100;
  53. s = s.length === 1 ? "0" + s : s;
  54. if (parseInt(s) < 10)
  55. s = "0" + s;
  56. return "00:" + h + ":" + s;
  57. }
  58. function timeToMilliseconds(timeString) {
  59. const [hours, minutes, secondsWithMs] = timeString.split(":");
  60. const [seconds, milliseconds] = secondsWithMs.split(".");
  61. const totalMilliseconds = parseInt(hours) * 3600 * 1e3 + // 小时转毫秒
  62. parseInt(minutes) * 60 * 1e3 + // 分钟转毫秒
  63. parseFloat(`${seconds}.${milliseconds}`) * 1e3;
  64. return totalMilliseconds;
  65. }
  66. function getTrackLength(str) {
  67. const milliseconds = timeToMilliseconds(str);
  68. return Math.round(milliseconds / 12.5);
  69. }
  70. const utils = {
  71. getDate,
  72. go,
  73. sToHs,
  74. timeToMilliseconds,
  75. getTrackLength
  76. };
  77. exports.go = go;
  78. exports.utils = utils;
  79. //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/utils.js.map