Date range by week number

1

With this simple function, I can get the week number. Now with the number of the week, how can I get the date range, started on Sunday?

import (
    "fmt"
    "time"
)

func main() {
    Week(time.Now().UTC())
}

func Week(now time.Time) string {
    _, thisWeek := now.ISOWeek()
    return "S" + strconv.Itoa(thisWeek)
}'

All help is welcome. Thank you.

    
asked by anonymous 12.09.2018 / 13:56

1 answer

0

My Portuguese is not good, sorry.

You can use time.(Time).Truncate and then change the result:

const (
    day       = 24 * time.Hour
    sevenDays = 7 * day
)

// weekSun returns the beginning and the end of the week of the time t in UTC,
// using Sunday as the first day.
func weekSun(t time.Time) (start, end time.Time) {
    start = t.Truncate(sevenDays).Add(-day)
    end = start.Add(sevenDays - 1)
    return start, end
}

Playground: link .

    
24.10.2018 / 00:48