我正在从数据库中检索start_date
和end_date
。通过for
循环,我将打印之间的日期(星期一,星期二,...... Soo)。我也有同样的情况我打印两个日期之间的日期,但我想如果我正在检索星期一的数据,那么它将显示即将到来的星期一而不是所有日子。如果我正在检索星期二的数据,那么它只显示我尝试过代码的星期二日期,但它会打印所有天我将如何获得预期的输出
代码: -
func GetSchedule(c *gin.Context) {
response := ResponseControllerList{}
selected_day:= "Monday"
conditions := bson.M{"day":selected_day}
data, err := models.GetSchedulingListing(conditions)
for i := range data {
start_date := data[i].StartDate
end_date := data[i].EndDate
start_time := data[i].StartTime
end_time := data[i].EndTime
start_working_hours := time.Unix(start_time,0).Format("3:04PM")
end_working_hours := time.Unix(end_time,0).Format("3:04PM")
days := time.Unix(end_date,0).Sub(time.Unix(start_date,0)).Hours() / 24
for i := 1; i <= int(days+1); i++ {
// fmt.Println(i)
// fmt.Println(start_date)
fmt.Println(time.Unix(start_date,0).Format("Monday, 01-02-2006")+" "+ start_working_hours+" "+ end_working_hours)
start_date = start_date + 86400
}
}
dataCount, err := models.GetRecordsCount(config.ScheduleCollection, conditions)
if err != nil {
response = ResponseControllerList{
config.FailureCode,
config.FailureFlag,
config.FailureMsg,
nil,
nil,
}
} else {
response = ResponseControllerList{
config.SuccessFlag,
config.SuccessFlag,
config.SuccessMsg,
data[0],
dataCount,
}
}
GetResponseList(c, response)
}
请参阅我在周一通过的条件,但它会在输出中返回所有日期: -
输出 -
Tuesday, 05-01-2018 8:00AM 10:00AM
Wednesday, 05-02-2018 8:00AM 10:00AM
Thursday, 05-03-2018 8:00AM 10:00AM
Friday, 05-04-2018 8:00AM 10:00AM
Saturday, 05-05-2018 8:00AM 10:00AM
Sunday, 05-06-2018 8:00AM 10:00AM
..Soo on up to end date
预期输出如果我给出的条件只有星期一那么: -
Monday, 05-07-2018 8:00AM 10:00AM
...Next Monday date
如果我在星期二给出条件
Tuesday, 05-01-2018 8:00AM 10:00AM
... next Tuesday date
数据库
{
"_id" : 1,
"day" : "Monday",
"start_date" : NumberLong(1525132800),
"end_date" : NumberLong(1527638400),
"start_time" : NumberLong(9000),
"end_time" : NumberLong(16200),
"added_on" : NumberLong(1526442426),
"occurence" : "repeat",
"updated_on" : NumberLong(0)
}
{
"_id" : 2,
"day" : "Monday",
"start_date" : NumberLong(1525132800),
"end_date" : NumberLong(1527638400),
"start_time" : NumberLong(27000),
"end_time" : NumberLong(34200),
"added_on" : NumberLong(1526442426),
"occurence" : "repeat",
"updated_on" : NumberLong(0)
}
{
"_id" : 3,
"day" : "Tuesday",
"start_date" : NumberLong(1525132800),
"end_date" : NumberLong(1527638400),
"start_time" : NumberLong(27000),
"end_time" : NumberLong(34200),
"added_on" : NumberLong(1526442426),
"occurence" : "repeat",
"updated_on" : NumberLong(0)
}
答案 0 :(得分:0)
我可以给你一些关于如何实现目标的提示:
获取满足给定日期的两个日期之间的星期几 predicadate?
请遵循此代码段,希望它能提供一些有关如何达到预期目标的线索。
这更直接: https://play.golang.org/p/-zQYNWtgHZ2
这有更多的设计决策: https://play.golang.org/p/qHlsCoQkSiQ
admitAllWeekDays
。 将答案放在一边,Slack,你需要的是整理一些你的代码......我发现你正在努力解决你提出的问题本身的问题。