| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- // 对小于 10 的补零
- export function zeroFill(val) {
- return val < 10 ? `0${val}` : val;
- }
- /**
- * 将秒转为时:分:秒格式
- * @param {Number|String} val 秒
- * @param {'normal'|'chinese'} type 格式类型
- * @returns {String} 'normal' hh:MM:ss 小于1小时返回 MM:ss 'chinese' h时m分s秒
- */
- export function secondFormatConversion(val = 0, type = 'normal') {
- const seconds = parseInt(val); // 输入的秒数
- const hours = Math.floor(seconds / 3600); // 小时部分
- const minutes = Math.floor((seconds % 3600) / 60); // 分钟部分
- const remainingSeconds = seconds % 60; // 剩余的秒数
- // 使用零填充函数来格式化小时、分钟和秒
- const formattedHours = zeroFill(hours);
- const formattedMinutes = zeroFill(minutes);
- const formattedSeconds = zeroFill(remainingSeconds);
- // 根据时间范围返回不同的格式
- if (hours > 0) {
- if (type === 'chinese') {
- return `${hours}时${minutes}分${remainingSeconds}秒`;
- }
- return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
- }
- if (type === 'chinese') {
- return `${minutes}分${remainingSeconds}秒`;
- }
- return `${formattedMinutes}:${formattedSeconds}`;
- }
- /**
- * 将时间转为 时:分:秒 格式
- * @param {Date} date 时间戳
- * @returns {String} hh:MM:ss
- */
- export function timeFormatConversion(date) {
- return `${zeroFill(date.getHours())}:${zeroFill(date.getMinutes())}:${zeroFill(date.getSeconds())}`;
- }
- /**
- * 将audio元素的currentTime转换为 分:秒:毫秒 格式
- * @param {Number} currentTime audio元素的currentTime属性(秒)
- * @returns {String} MM:ss:SSS
- * @description 将以秒为单位的浮点数转换为分:秒:毫秒格式
- */
- export function audioTimeToMMSS(currentTime) {
- const totalMilliseconds = Math.floor(currentTime * 1000);
- const minutes = zeroFill(Math.floor(totalMilliseconds / 60000));
- const seconds = zeroFill(Math.floor((totalMilliseconds % 60000) / 1000));
- const milliseconds = String(totalMilliseconds % 1000).padStart(3, '0');
- return `${minutes}:${seconds}:${milliseconds}`;
- }
- // 分:秒转秒
- export function timeStrToSen(time) {
- if (!time) {
- return -1;
- }
- let pos = time.indexOf(':');
- let min = 0;
- let sec = 0;
- if (pos > 0) {
- min = parseInt(time.substring(0, pos));
- sec = parseFloat(time.substring(pos + 1));
- }
- return min * 60 + sec;
- }
|