doujinai2183 2018-05-16 12:45
浏览 189

如何使用golang打印两个日期之间的特定日期?

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)
}
  • 写回答

1条回答 默认 最新

  • duan33360 2018-05-16 14:05
    关注

    I can leave you some hints about how you can archieve the goal of :

    get the days of the week between two dates that satisfies a given predicadate?

    Follow this code snippet, hope it can give some clues about how to archieve the desired goal.

    This is more straight ahead: https://play.golang.org/p/-zQYNWtgHZ2

    This has a little more design decisions: https://play.golang.org/p/qHlsCoQkSiQ

    • Please do notice that you should implement you day of week filtering/selection like the func admitAllWeekDays.
    • Also notice that the day of week has its implementation based on as an integer.

    Leaving the answer aside, Slack, what you need is to organize a little bit your code.. i see that you are struggling a little more that the problem itself that you present as the question.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。