duanfan5012 2018-09-12 17:32
浏览 272

日期范围按周数Golang

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)
}

Any help is welcome. Thank you.

  • 写回答

1条回答 默认 最新

  • dongyi2006 2018-09-12 21:29
    关注

    Foreword: Time.ISOWeek() returns you the week number that starts on Monday, so I will answer your question that also handles weeks starting on Monday. Alter it to your needs if you want it to work with weeks starting on Sunday.

    The standard library does not provide a function that would return you the date range of a given week (year+week number). So we have to construct one ourselves.

    And it's not that hard. We can start off from the middle of the year, align to the first day of the week (Monday), get the week of this time value, and corrigate: add as many days as the week difference multiplied by 7.

    This is how it could look like:

    func WeekStart(year, week int) time.Time {
        // Start from the middle of the year:
        t := time.Date(year, 7, 1, 0, 0, 0, 0, time.UTC)
    
        // Roll back to Monday:
        if wd := t.Weekday(); wd == time.Sunday {
            t = t.AddDate(0, 0, -6)
        } else {
            t = t.AddDate(0, 0, -int(wd)+1)
        }
    
        // Difference in weeks:
        _, w := t.ISOWeek()
        t = t.AddDate(0, 0, (week-w)*7)
    
        return t
    }
    

    Testing it:

    fmt.Println(WeekStart(2018, 1))
    fmt.Println(WeekStart(2018, 2))
    fmt.Println(WeekStart(2019, 1))
    fmt.Println(WeekStart(2019, 2))
    

    Output (try it on the Go Playground):

    2018-01-01 00:00:00 +0000 UTC
    2018-01-08 00:00:00 +0000 UTC
    2018-12-31 00:00:00 +0000 UTC
    2019-01-07 00:00:00 +0000 UTC
    

    One nice property of this WeekStart() implementation is that it handles out-of-range weeks nicely. That is, if you pass 0 for the week, it will be interpreted as the last week of the previous year. If you pass -1 for the week, it will designate the second to last week of the previous year. Similarly, if you pass max week of the year plus 1, it will be interpreted as the first week of the next year etc.

    The above WeekStart() function only returns the given week's first day (Monday), because the last day of the week is always its first day + 6 days.

    If we also need the last day:

    func WeekRange(year, week int) (start, end time.Time) {
        start = WeekStart(year, week)
        end = start.AddDate(0, 0, 6)
        return
    }
    

    Testing it:

    fmt.Println(WeekRange(2018, 1))
    fmt.Println(WeekRange(2018, 2))
    fmt.Println(WeekRange(2019, 1))
    fmt.Println(WeekRange(2019, 2))
    

    Output (try it on the Go Playground):

    2018-01-01 00:00:00 +0000 UTC 2018-01-07 00:00:00 +0000 UTC
    2018-01-08 00:00:00 +0000 UTC 2018-01-14 00:00:00 +0000 UTC
    2018-12-31 00:00:00 +0000 UTC 2019-01-06 00:00:00 +0000 UTC
    2019-01-07 00:00:00 +0000 UTC 2019-01-13 00:00:00 +0000 UTC
    
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用