go 获取是第几周,日期范围按周数Golang

这篇博客介绍了一个Golang函数,用于根据年份和周数获取该周的起始和结束日期。函数首先从每年的7月1日开始,调整到周一,然后根据输入的周数计算出日期范围。
摘要由CSDN通过智能技术生成

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值