英文:
golang action on specific days of the week (ie. every Friday)
问题
我需要在一个时间段内获取特定的星期几,比如在2015-08-17到2015-10-14之间的每个星期日,所以我得到了以下代码。
onDate, err := time.Parse(dateFormat, startDate)
if err != nil {
logr.Println(err)
}
offDate, err := time.Parse(dateFormat, stopDate)
if err != nil {
logr.Println(err)
}
weekday := onDate.Weekday()
getDay := int(weekday)
a := onDate.YearDay()
b := offDate.YearDay()
var date string
var z time.Time
// Sundaytime1 is entered in the browser and will always be the same
if sundayTime1 != "" && getDay == 0 {
for a <= b {
z := onDate.AddDate(0, 0, 7)
date := time.Parse(dateFormat, z)
fmt.Println(onDate)
_, err = que.Exec(studyID, msgid, date, sundayTime1, 0, 0)
if err != nil {
fmt.Println(err)
}
fmt.Println("I'm a Sunday!")
a = a + 7
}
}
这段代码会递增日期,但只会执行一次,所以会重复返回2015-08-23 00:00:00 +0000 UTC
。我无法让它只返回2015-08-23
,但它确实只会在每个星期日进行一次数据库记录。但是日期是错误的,而且不应该包含时间。缺点是我对每个可能的星期开始日期都有一个类似的代码。有没有更好的处理方法,以及如何只获取2015-08-23
而不包含时间。
英文:
I need to get a specific day of the week inbetween a period such as for every sunday between 2015-08-17 to 2015-10-14 so I've end up with this.
onDate, err := time.Parse(dateFormat, startDate)
if err != nil {
logr.Println(err)
}
offDate, err := time.Parse(dateFormat, stopDate)
if err != nil {
logr.Println(err)
}
weekday := onDate.Weekday()
getDay := int(weekday)
a := onDate.YearDay()
b := offDate.YearDay()
var date string
var z time.Time
// Sundaytime1 is entered in the browser and will always be the same
if sundayTime1 != "" && getDay == 0 {
for a <= b {
z := onDate.AddDate(0, 0, 7)
date := time.Parse(dateFormat, z)
fmt.Println(onDate)
_, err = que.Exec(studyID, msgid, date, sundayTime1, 0, 0)
if err != nil {
fmt.Println(err)
}
fmt.Println("I'm a Sunday!")
a = a + 7
}
}
This does increment the day by on but it only does it once so it returns 2015-08-23 00:00:00 +0000 UTC
repeatedly. and i can't seem to get it to only return 2015-08-23
but it does only perform a database entry for every Sunday between the time. but its the wrong date and it shouldn't include the time. The downside being i have one of these for every possible starting day of the week. Is there a better way to handle this and how i only get 2015-08-23
without the time.
答案1
得分: 1
这可能适用于你:http://play.golang.org/p/UGnV8kPrA3
由于你正在处理时间实例,结束日期在语义上是时间实例2015-10-14T00:00:00
之前的日期加上1天。
英文:
this might work for you: http://play.golang.org/p/UGnV8kPrA3
as you are dealing with instances in time, the end date is semantically the date before the time instance 2015-10-14T00:00:00
+ 1 day
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论