How to convert hh:mm:ss to minutes in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strconv"
    "strings"
)

func hhmmsstominutes(hhmmss string) float64 {
    timeParts := strings.Split(hhmmss, ":")
    
    hours, _ := strconv.Atoi(timeParts[0])
    minutes, _ := strconv.Atoi(timeParts[1])
    seconds, _ := strconv.Atoi(timeParts[2])

    return float64(hours*60) + float64(minutes) + float64(seconds)/60
}

func main() {
    fmt.Println(hhmmsstominutes("2:30:00"))
    fmt.Println(hhmmsstominutes("2:35:30"))
    fmt.Println(hhmmsstominutes("5:00:45"))
}



/*
run:

150
155.5
300.75

*/

 



answered Apr 17, 2025 by avibootz

Related questions

1 answer 74 views
1 answer 90 views
1 answer 115 views
1 answer 98 views
1 answer 112 views
1 answer 184 views
1 answer 118 views
...