go 获取是第几周_golang获取当前时间是第几周

本文展示了如何在Go(Golang)中计算指定日期是当年的第几周,通过`time`包进行时间操作,实现按周划分日期范围的功能。
摘要由CSDN通过智能技术生成

Go语言中使用import关键字导入包,包的名字使用双引号("")包裹起来。

golang判断当前时间是第几周:

func main() {l, _ := time.LoadLocation("Asia/Shanghai")startTime,_ := time.ParseInLocation("2006-01-02", "2018-12-22", l)endTime,_ := time.ParseInLocation("2006-01-02", "2019-05-17", l)datas := GroupByWeekDate(startTime, endTime)for _, d := range datas {fmt.Println(d)}}//判断时间是当年的第几周func WeekByDate(t time.Time) string {yearDay := t.YearDay()yearFirstDay := t.AddDate(0, 0, -yearDay+1)firstDayInWeek := int(yearFirstDay.Weekday())//今年第一周有几天firstWeekDays := 1if firstDayInWeek != 0 {firstWeekDays = 7 - firstDayInWeek + 1}var week intif yearDay <= firstWeekDays {week = 1} else {week = (yearDay-firstWeekDays)/7 + 2}return fmt.Sprintf("%d第%d周", t.Year(), week)}type WeekDate struct {WeekTh stringStartTime time.TimeEndTime time.Time}// 将开始时间和结束时间分割为周为单位func GroupByWeekDate(startTime, endTime time.Time) []WeekDate {weekDate := make([]WeekDate, 0)diffDuration := endTime.Sub(startTime)days := int(math.Ceil(float64(diffDuration / (time.Hour * 24))))+1currentWeekDate := WeekDate{}currentWeekDate.WeekTh = WeekByDate(endTime)currentWeekDate.EndTime = endTimecurrentWeekDay := int(endTime.Weekday())if currentWeekDay == 0 {currentWeekDay = 7}currentWeekDate.StartTime = endTime.AddDate(0, 0, -currentWeekDay+1)nextWeekEndTime := currentWeekDate.StartTimeweekDate = append(weekDate, currentWeekDate)for i := 0; i < (days-currentWeekDay)/7; i++ {weekData := WeekDate{}weekData.EndTime = nextWeekEndTimeweekData.StartTime = nextWeekEndTime.AddDate(0, 0, -7)weekData.WeekTh = WeekByDate(weekData.StartTime)nextWeekEndTime = weekData.StartTimeweekDate = append(weekDate, weekData)}if lastDays := (days - currentWeekDay) % 7; lastDays > 0 {lastData := WeekDate{}lastData.EndTime = nextWeekEndTimelastData.StartTime = nextWeekEndTime.AddDate(0, 0, - lastDays)lastData.WeekTh = WeekByDate(lastData.StartTime)weekDate = append(weekDate, lastData)}return weekDate}

结果:

{2019第20周 2019-05-13 00:00:00 +0800 CST 2019-05-17 00:00:00 +0800 CST}{2019第19周 2019-05-06 00:00:00 +0800 CST 2019-05-13 00:00:00 +0800 CST}{2019第18周 2019-04-29 00:00:00 +0800 CST 2019-05-06 00:00:00 +0800 CST}{2019第17周 2019-04-22 00:00:00 +0800 CST 2019-04-29 00:00:00 +0800 CST}{2019第16周 2019-04-15 00:00:00 +0800 CST 2019-04-22 00:00:00 +0800 CST}{2019第15周 2019-04-08 00:00:00 +0800 CST 2019-04-15 00:00:00 +0800 CST}{2019第14周 2019-04-01 00:00:00 +0800 CST 2019-04-08 00:00:00 +0800 CST}{2019第13周 2019-03-25 00:00:00 +0800 CST 2019-04-01 00:00:00 +0800 CST}{2019第12周 2019-03-18 00:00:00 +0800 CST 2019-03-25 00:00:00 +0800 CST}{2019第11周 2019-03-11 00:00:00 +0800 CST 2019-03-18 00:00:00 +0800 CST}{2019第10周 2019-03-04 00:00:00 +0800 CST 2019-03-11 00:00:00 +0800 CST}{2019第9周 2019-02-25 00:00:00 +0800 CST 2019-03-04 00:00:00 +0800 CST}{2019第8周 2019-02-18 00:00:00 +0800 CST 2019-02-25 00:00:00 +0800 CST}{2019第7周 2019-02-11 00:00:00 +0800 CST 2019-02-18 00:00:00 +0800 CST}{2019第6周 2019-02-04 00:00:00 +0800 CST 2019-02-11 00:00:00 +0800 CST}{2019第5周 2019-01-28 00:00:00 +0800 CST 2019-02-04 00:00:00 +0800 CST}{2019第4周 2019-01-21 00:00:00 +0800 CST 2019-01-28 00:00:00 +0800 CST}{2019第3周 2019-01-14 00:00:00 +0800 CST 2019-01-21 00:00:00 +0800 CST}{2019第2周 2019-01-07 00:00:00 +0800 CST 2019-01-14 00:00:00 +0800 CST}{2018第53周 2018-12-31 00:00:00 +0800 CST 2019-01-07 00:00:00 +0800 CST}{2018第52周 2018-12-24 00:00:00 +0800 CST 2018-12-31 00:00:00 +0800 CST}{2018第51周 2018-12-22 00:00:00 +0800 CST 2018-12-24 00:00:00 +0800 CST}

更多golang知识请关注golang教程栏目。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值