发布于 4年前

vue怎么实现24小时的时间加减?

在不用插件的情况下,怎么写时间加减的逻辑?

<div v-model="time1">{{ time1 }}</div>
<div v-model="time2">{{ time1 }}</div>

<div @click="AddTime()">{{ hours }}</div>
AddTime(){
    this.hours= Number(this.time1)+Number(this.time2);

    console.log(hours)
}

这样出来只能是 20+5=25,但是我需要的是20加5小时,返回应该是1点

不用插件,只用了script引用的vue,应该要怎么写才能正确加减时间?

最佳答案

相加的结果,再对24取余

AddTime(){
    this.hours= (Number(this.time1)+Number(this.time2))%24;

    console.log(hours)
}
©2020 edoou.com   京ICP备16001874号-3