Vue3将时分秒转化为秒数
时间: 2023-08-11 20:06:57 浏览: 333
在 Vue3 中,可以使用 Date 对象,将时分秒转化为秒数。具体步骤如下:
1. 创建一个新的 Date 对象,将时分秒作为参数传入。
2. 使用 Date 对象的 getTime 方法,将时间转化为毫秒数。
3. 将毫秒数除以1000,得到秒数。
以下是实现代码:
```html
<template>
<div>
01:23:45 转化为秒数为 {{ totalSeconds }} 秒
</div>
</template>
<script>
export default {
data() {
return {
totalSeconds: 0
}
},
mounted() {
const time = new Date('1970-01-01T01:23:45')
this.totalSeconds = time.getTime() / 1000
}
}
</script>
```
运行结果为:
```
01:23:45 转化为秒数为 5025 秒
```
注意,在创建 Date 对象时,日期部分可以填任意值,因为我们只需要时分秒的信息。
相关问题
vue2 dayjs 将秒转换成时分秒格式
可以使用Day.js插件和JavaScript的Date对象来将秒转换成时分秒格式。在Vue2项目中,我们可以使用以下代码实现:
```
import dayjs from 'dayjs'
// seconds为需要转换的秒数
function formatSeconds(seconds) {
const minute = 60
const hour = 60 * minute
const minuteStr = dayjs().minute(~~(seconds / minute)).format('mm')
const secondStr = dayjs().second(seconds % 60).format('ss')
const hourStr = dayjs().hour(~~(seconds / hour)).format('HH')
return `${hourStr}:${minuteStr}:${secondStr}`
}
```
在这里,我们将秒数分别转换为小时数、分钟数和剩余的秒数,并使用Day.js格式化时间。
vue2 将秒转换成时分秒格式
可以使用moment.js库来将秒转换成时分秒格式,具体代码如下:
```
import moment from 'moment'
let second = 3661 // 需要转换的秒数
let duration = moment.duration(second, 'seconds')
let hours = duration.hours()
let minutes = duration.minutes()
let seconds = duration.seconds()
let timeFormat = ''
if (hours > 0) {
timeFormat += hours + ':'
}
timeFormat += (minutes < 10 ? '0' + minutes : minutes) + ':'
timeFormat += (seconds < 10 ? '0' + seconds : seconds)
console.log(timeFormat) // 输出:1:01:01
```
阅读全文
相关推荐














