日期范围按周数Golang [英] Date range by week number Golang

查看:561
本文介绍了日期范围按周数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)
}

欢迎任何帮助。谢谢。

推荐答案

前言: Time.ISOWeek() 返回从星期一开始的星期数,因此我将会回答您的问题,该问题也将在星期一开始处理数周。如果您希望它在周日开始的几周内工作,请根据需要进行更改。

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.

我在 github.com/icza/gox ,请参见 timex.WeekStart()

标准库不提供会返回给定星期(年+周数)的日期范围的函数。因此,我们必须自己构建自己。

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.

这并不难。我们可以从年中开始,与一周的第一天(星期一)对齐,获取该时间值中的星期,然后修正:将与星期差乘以7的天数相加。

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.

这是它的样子:

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
}

测试

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

输出(在转到游乐场上尝试) :

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

WeekStart()实现的一个不错的特性是它可以处理 -range 周。也就是说,如果您在一周中通过了 0 ,则它将被解释为上一年的最后一周。如果您在本周通过了 -1 ,它将指定上一年的第二个到最后一个星期。同样,如果您通过一年中的最大周数加1,则将其解释为明年的第一周,依此类推。

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.

上面的 WeekStart()函数仅返回给定星期的第一天(星期一),因为一周的最后一天始终是其第一天+ 6天。

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
}

对其进行测试:

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

输出(在去操场):

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

这篇关于日期范围按周数Golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆