2024-06-27 16:24:41 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 将时间字符串转换为毫秒(number)
|
|
|
|
|
|
* 00:00:03.867 --》3867
|
|
|
|
|
|
* @param {*} timeString 时间字符串
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function TimeStringToMilliseconds(timeString) {
|
|
|
|
|
|
// 分割字符串获取小时、分钟、秒和毫秒
|
|
|
|
|
|
const parts = timeString.split(/[:.]/)
|
|
|
|
|
|
const hours = parseInt(parts[0], 10)
|
|
|
|
|
|
const minutes = parseInt(parts[1], 10)
|
|
|
|
|
|
const seconds = parseInt(parts[2], 10)
|
|
|
|
|
|
const milliseconds = parseInt(parts[3], 10)
|
|
|
|
|
|
|
|
|
|
|
|
// 将小时、分钟、秒转换为毫秒并计算总和
|
|
|
|
|
|
return hours * 3600000 + minutes * 60000 + seconds * 1000 + milliseconds
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 将毫秒转换为时间字符串
|
|
|
|
|
|
* 85233 --》 '00:01:25.233'
|
|
|
|
|
|
* @param {*} milliseconds
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function MillisecondsToTimeString(milliseconds) {
|
|
|
|
|
|
let totalSeconds = milliseconds / 1000
|
|
|
|
|
|
const hours = Math.floor(totalSeconds / 3600)
|
|
|
|
|
|
totalSeconds %= 3600
|
|
|
|
|
|
const minutes = Math.floor(totalSeconds / 60)
|
|
|
|
|
|
const seconds = Math.floor(totalSeconds % 60)
|
|
|
|
|
|
const ms = milliseconds % 1000
|
|
|
|
|
|
|
|
|
|
|
|
// 将小时、分钟、秒格式化为两位数,毫秒格式化为三位数
|
|
|
|
|
|
const hoursFormatted = hours.toString().padStart(2, '0')
|
|
|
|
|
|
const minutesFormatted = minutes.toString().padStart(2, '0')
|
|
|
|
|
|
const secondsFormatted = seconds.toString().padStart(2, '0')
|
|
|
|
|
|
const msFormatted = ms.toString().padStart(3, '0')
|
|
|
|
|
|
|
2024-07-13 15:44:13 +08:00
|
|
|
|
let timeString = `${hoursFormatted}:${minutesFormatted}:${secondsFormatted}.${msFormatted}`
|
|
|
|
|
|
|
|
|
|
|
|
// 使用正则表达式检测并删除多余的小数点
|
|
|
|
|
|
// 此正则表达式查找除了第一个小数点之外的所有小数点,并将它们替换为空字符串
|
|
|
|
|
|
timeString = timeString.replace(/(\.\d+)\./g, '$1')
|
|
|
|
|
|
return timeString
|
2024-06-27 16:24:41 +08:00
|
|
|
|
}
|