91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

golang判斷當前時間是第幾周的方法

發布時間:2020-05-23 17:07:41 來源:億速云 閱讀:1323 作者:鴿子 欄目:編程語言

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 := 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
}

// 將開始時間和結束時間分割為周為單位
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
}

結果:

{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獲取當前時間是第幾周的詳細內容,更多請關注億速云其它相關文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

明水县| 吴江市| 扶沟县| 平凉市| 阳东县| 同德县| 黄浦区| 海晏县| 沭阳县| 蕲春县| 时尚| 乌审旗| 曲水县| 阜阳市| 仁化县| 铜川市| 永昌县| 桃江县| 怀化市| 贵溪市| 延津县| 新昌县| 满洲里市| 塔河县| 武乡县| 嘉黎县| 南阳市| 漠河县| 特克斯县| 通许县| 民和| 永宁县| 长沙县| 武威市| 金寨县| 昌吉市| 新泰市| 剑河县| 石景山区| 公主岭市| 武隆县|