Go语言 将秒转换为具体的时间


  使用一个数值表示时间中的“秒”值,然后使用 resolveTime() 函数将传入的秒数转换为天、小时和分钟等时间单位。

package main

import "fmt"

/*
时间常量
*/
const (

    //定义每分钟的秒数
    SecondsPerMinute = 60
    //定义每小时的秒数
    SecondsPerHour = SecondsPerMinute * 60
    //定义每天的秒数
    SecondsPerDay = SecondsPerHour * 24
)

/*
时间转换函数
*/
func resolveTime(seconds int) (day int, hour int, minute int) {
    //每分钟秒数
    minute = seconds / SecondsPerMinute
    //每小时秒数
    hour = seconds / SecondsPerHour
    //每天秒数
    day = seconds / SecondsPerDay
    return
}

func main() {
    //打印返回参数
    fmt.Println(resolveTime(1000))
    //只打印小时和分钟
    _, hour, minute := resolveTime(1800)
    fmt.Println(hour, minute)
    //只打印天
    day, _, _ := resolveTime(90000)
    fmt.Println(day)
}

程序输出:

0 0 16
0 30
1

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM