gpt4 book ai didi

date - 如何使用golang打印两个日期之间的具体日期?

转载 作者:IT王子 更新时间:2023-10-29 02:20:50 25 4
gpt4 key购买 nike

我正在从数据库中检索 start_dateend_date。通过 for 循环,我将打印之间的日期(星期一、星期二、...等等)。我也有同样的情况,我打印两个日期之间的日期,但我想如果我正在检索星期一的数据,那么它将显示接下来的星期一而不是所有天。如果我正在检索星期二的数据,那么它只显示星期二的日期我已经尝试了一个代码,但它会整天打印我将如何获得预期的输出

代码:-

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

预期输出如果我给出条件 That only Monday 那么:-

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)
}

最佳答案

我可以给你一些关于如何实现目标的提示:

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

按照这个代码片段,希望它能提供一些关于如何实现预期目标的线索。

这是更直接的: https://play.golang.org/p/-zQYNWtgHZ2

这有更多的设计决策: https://play.golang.org/p/qHlsCoQkSiQ

  • 请注意您应该实现星期几像 func admitAllWeekDays 这样的过滤/选择。
  • 另请注意,星期几的实现基于整数。

把答案放在一边,Slack,你需要的是稍微组织一下你的代码。我看到你比你提出的问题本身更挣扎。

关于date - 如何使用golang打印两个日期之间的具体日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50371350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com