一、将04:00小时,转换为240分钟(互转)
<template>
<a-time-picker
:value="duration | timeFilter"
format="HH:mm"
placeholder="请选择时间"
@change="timeOnChange"
/>
</template>
<script>
import moment from 'moment'
export default {
data() {
return {
duration:"",
},
},
methods: {
moment,
timeOnChange(time, timeString) {
// 举例:将04:00转为240分钟这样的格式
const duration = moment.duration(timeString)
const minutes = duration.asMinutes();
console.log(minutes); // 240
}
},
filters:{
timeFilter(val){
if(val){
//举例:将240分钟转为04:00这样的格式
const duration = moment.duration(val, 'minutes');
const formattedTime = moment.utc(duration.asMilliseconds()).format('HH:mm');
return moment(formattedTime, 'HH:mm')
}else{
return moment('00:00', 'HH:mm')
}
}
},
</script>