I'm retrieving the start_date
and end_date
from the db. Through for
loop I will print the dates between like (Monday, Tuesday,... Soo on). I have also a same situation I print the dates between two dates but I want if I'm retrieving the data of Monday then it will show me the coming Mondays not all days. If I'm retrieving the data of the Tuesday then it only show me the Tuesday dates I have tried a code but it will print all days how will I get the expected output
code:-
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)
}
See in condition I pass Monday but it return all days in output:-
Output-
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
Expected output If I give condition That only Monday then:-
Monday, 05-07-2018 8:00AM 10:00AM
...Next Monday date
If I give in condition Tuesday then
Tuesday, 05-01-2018 8:00AM 10:00AM
... next Tuesday date
Database
{
"_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)
}