重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

golang判断当前时间是第几周的方法

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

成都一家集口碑和实力的网站建设服务商,拥有专业的企业建站团队和靠谱的建站技术,十载企业及个人网站建设经验 ,为成都上1000家客户提供网页设计制作,网站开发,企业网站制作建设等服务,包括成都营销型网站建设,高端网站设计,同时也为不同行业的客户提供网站制作、做网站的服务,包括成都电商型网站制作建设,装修行业网站制作建设,传统机械行业网站建设,传统农业行业网站制作建设。在成都做网站,选网站制作建设服务商就选创新互联公司

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获取当前时间是第几周的详细内容,更多请关注创新互联其它相关文章!


网页标题:golang判断当前时间是第几周的方法
浏览路径:http://cqcxhl.com/article/ipoijg.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP