How to convert days into human-readable weeks and days in Go

1 Answer

0 votes
package main

import (
    "fmt"
)

// Convert total days into a human‑readable "X weeks and Y days" string
func ToReadableWeeksDays(totalDays int) string {
    weeks := totalDays / 7   // whole weeks
    days := totalDays % 7    // leftover days

    return fmt.Sprintf("%d week%s and %d day%s",
        weeks,
        func() string { if weeks == 1 { return "" } else { return "s" } }(),
        days,
        func() string { if days == 1 { return "" } else { return "s" } }(),
    )
}

func main() {
    days := 26
    
    fmt.Println(ToReadableWeeksDays(days))
}



/*
run:

3 weeks and 5 days

*/

 



answered Dec 31, 2025 by avibootz

Related questions

...