Go語言中使用import關(guān)鍵字導(dǎo)入包,包的名字使用雙引號(hào)("")包裹起來。
成都一家集口碑和實(shí)力的網(wǎng)站建設(shè)服務(wù)商,擁有專業(yè)的企業(yè)建站團(tuán)隊(duì)和靠譜的建站技術(shù),十載企業(yè)及個(gè)人網(wǎng)站建設(shè)經(jīng)驗(yàn) ,為成都上1000家客戶提供網(wǎng)頁設(shè)計(jì)制作,網(wǎng)站開發(fā),企業(yè)網(wǎng)站制作建設(shè)等服務(wù),包括成都營(yíng)銷型網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),同時(shí)也為不同行業(yè)的客戶提供網(wǎng)站制作、做網(wǎng)站的服務(wù),包括成都電商型網(wǎng)站制作建設(shè),裝修行業(yè)網(wǎng)站制作建設(shè),傳統(tǒng)機(jī)械行業(yè)網(wǎng)站建設(shè),傳統(tǒng)農(nóng)業(yè)行業(yè)網(wǎng)站制作建設(shè)。在成都做網(wǎng)站,選網(wǎng)站制作建設(shè)服務(wù)商就選創(chuàng)新互聯(lián)公司。
golang判斷當(dāng)前時(shí)間是第幾周:
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) } } //判斷時(shí)間是當(dāng)年的第幾周 func WeekByDate(t time.Time) string { yearDay := t.YearDay() yearFirstDay := t.AddDate(0, 0, -yearDay+1) firstDayInWeek := int(yearFirstDay.Weekday()) //今年第一周有幾天 firstWeekDays := 1 if firstDayInWeek != 0 { firstWeekDays = 7 - firstDayInWeek + 1 } var week int if yearDay <= firstWeekDays { week = 1 } else { week = (yearDay-firstWeekDays)/7 + 2 } return fmt.Sprintf("%d第%d周", t.Year(), week) } type WeekDate struct { WeekTh string StartTime time.Time EndTime time.Time } // 將開始時(shí)間和結(jié)束時(shí)間分割為周為單位 func GroupByWeekDate(startTime, endTime time.Time) []WeekDate { weekDate := make([]WeekDate, 0) diffDuration := endTime.Sub(startTime) days := int(math.Ceil(float64(diffDuration / (time.Hour * 24))))+1 currentWeekDate := WeekDate{} currentWeekDate.WeekTh = WeekByDate(endTime) currentWeekDate.EndTime = endTime currentWeekDay := int(endTime.Weekday()) if currentWeekDay == 0 { currentWeekDay = 7 } currentWeekDate.StartTime = endTime.AddDate(0, 0, -currentWeekDay+1) nextWeekEndTime := currentWeekDate.StartTime weekDate = append(weekDate, currentWeekDate) for i := 0; i < (days-currentWeekDay)/7; i++ { weekData := WeekDate{} weekData.EndTime = nextWeekEndTime weekData.StartTime = nextWeekEndTime.AddDate(0, 0, -7) weekData.WeekTh = WeekByDate(weekData.StartTime) nextWeekEndTime = weekData.StartTime weekDate = append(weekDate, weekData) } if lastDays := (days - currentWeekDay) % 7; lastDays > 0 { lastData := WeekDate{} lastData.EndTime = nextWeekEndTime lastData.StartTime = nextWeekEndTime.AddDate(0, 0, - lastDays) lastData.WeekTh = WeekByDate(lastData.StartTime) weekDate = append(weekDate, lastData) } return weekDate }
結(jié)果:
{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獲取當(dāng)前時(shí)間是第幾周的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!
文章標(biāo)題:golang判斷當(dāng)前時(shí)間是第幾周的方法
標(biāo)題網(wǎng)址:http://aaarwkj.com/article26/ipoijg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、電子商務(wù)、Google、虛擬主機(jī)、動(dòng)態(tài)網(wǎng)站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)