| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- // 获取任意时间
- export const getDate = (date, AddDayCount = 0) => {
- if (!date) {
- date = new Date();
- }
- if (typeof date !== 'object') {
- date = date.replace(/-/g, '/');
- }
- const dd = new Date(date);
- dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
- const y = dd.getFullYear();
- const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期,不足10补0
- const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号,不足10补0
- const dayStrList = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
- 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()
- }`;
- return {
- fullDate: y + '-' + m + '-' + d,
- year: y,
- month: m,
- date: d,
- day: dd.getDay(),
- dayStr: dayStrList[dd.getDay()],
- rStr: `${m}月${d}日`,
- HMS
- };
- };
- // tabBar 页面路径(与 pages.json 一致),跳转这些页面须用 switchTab,不能用 redirectTo/navigateTo
- const TABBAR_PATHS = ['/pages/index/index', '/pages/numberOfDiners/index', '/pages/personal/index'];
- // 通用跳转方法
- export function go(url, mode = 'navigateTo') {
- if (url == -1) {
- let pages = getCurrentPages(); //获取页面
- let beforePage = pages[pages.length - 2]; //上个页面
- if (beforePage) {
- uni.navigateBack();
- } else {
- uni.switchTab({ url: '/pages/index/index' });
- }
- } else {
- const path = typeof url === 'string' ? url.split('?')[0] : '';
- const isTabBar = TABBAR_PATHS.some((p) => path === p || path.endsWith(p));
- const actualMode = isTabBar ? 'switchTab' : mode;
- uni[actualMode]({ url });
- }
- }
- export function sToHs(s) {
- //计算分钟
- //算法:将秒数除以60,然后下舍入,既得到分钟数
- let h;
- h = Math.floor(s / 60);
- //计算秒
- //算法:取得秒%60的余数,既得到秒数
- s = s % 60;
- //将变量转换为字符串
- h += '';
- s += '';
- //如果只有一位数,前面增加一个0
- h = h.length === 1 ? '0' + h : h;
- s = Math.floor(s * 100) / 100;
- s = s.length === 1 ? '0' + s : s;
- if (parseInt(s) < 10) s = '0' + s;
- return '00:' + h + ':' + s;
- }
- // 将时间格式'00:00:00.00' 转成毫秒
- export function timeToMilliseconds(timeString) {
- // 拆分时间字符串
- const [hours, minutes, secondsWithMs] = timeString.split(':');
- const [seconds, milliseconds] = secondsWithMs.split('.');
- // 转换为秒数
- const totalMilliseconds =
- parseInt(hours) * 3600 * 1000 + // 小时转毫秒
- parseInt(minutes) * 60 * 1000 + // 分钟转毫秒
- parseFloat(`${seconds}.${milliseconds}`) * 1000; // 秒和毫秒转毫秒
- return totalMilliseconds;
- }
- // 将毫秒转长度
- export function getTrackLength(str) {
- const milliseconds = timeToMilliseconds(str);
- return Math.round(milliseconds / 12.5);
- }
- export default {
- getDate,
- go,
- sToHs,
- timeToMilliseconds,
- getTrackLength
- };
|