go 格式化秒
package main import ( "fmt" ) func SecondFormat(diffSec int) (timeStr string) { if diffSec <= 60 { //多少秒 timeStr = fmt.Sprintf("%d秒", diffSec) } else if diffSec > 60 && diffSec < 3600 { minute := diffSec / 60 second := diffSec % 60 //多少分 timeStr = fmt.Sprintf("%d分%d秒", minute, second) } else if diffSec > 3600 && diffSec < 86400 { //多少小时 hour := diffSec / 3600 minute := diffSec % 3600 / 60 second := diffSec % 3600 % 60 timeStr = fmt.Sprintf("%d小时%d分%d秒", hour, minute, second) } else if diffSec > 86400 && diffSec < 86400*30 { //多少天 day := diffSec / 86400 hour := diffSec % 86400 / 3600 minute := diffSec % 86400 % 3600 / 60 second := diffSec % 86400 % 3600 % 60 timeStr = fmt.Sprintf("%d天%d小时%d分%d秒", day, hour, minute, second) } return } func main() { fmt.Println(SecondFormat(10)) fmt.Println(SecondFormat(60)) fmt.Println(SecondFormat(61)) fmt.Println(SecondFormat(3601)) fmt.Println(SecondFormat(86403)) }
posted on 2021-11-16 09:57 running-fly 阅读(37) 评论(0) 编辑 收藏 举报