当前位置: > > > > 如何检查rest api中的截止日期是否大于明天
来源:stackoverflow
2024-04-20 15:30:35
0浏览
收藏
大家好,我们又见面了啊~本文《如何检查rest api中的截止日期是否大于明天》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~
问题内容
我已经编写了一个待办事项处理程序 api,并想添加一个条件来检查用户输入的 duedate 是否小于明天日期,我应该如何编写它而不是???在下面的代码中?
type Todo struct { gorm.Model Title string `json:"title,omitempty"` Description string `json:"description,omitempty"` DueDate time.Time `json:"duedate,omitempty"` UserId uint `json:"user_id"` //The user that this todo belongs to } func ValidateTodo(todo *Todo) (map[string]interface{}, bool) { if todo.Title == "" { return u.Message(false, "Todo title should be on the payload"), false } if len(todo.Title)>30 { return u.Message(false, "Todo title should be less than 30 characters"), false } if todo.Description == "" { return u.Message(false, "Todo description should be on the payload"), false } if todo.DueDate<time.Now(){ ????? } return u.Message(true, "success"), true }
解决方案
您可以使用 和 的组合来执行此操作
duedate := time.now() tomorrow := time.now().adddate(0, 0, 1) if tomorrow.before(duedate) { fmt.println("tomorrow is before your due date") }
编辑:沃尔克的评论实际上提出了一个很好的观点。为了可读性,使用 可能更符合逻辑。所以你可以执行以下操作;
duedate := time.now() tomorrow := time.now().adddate(0, 0, 1) if duedate.after(tomorrow) { fmt.println("due date is after tomorrow") }
编辑2:根据希望明天的时间为 00:00:00 的愿望,将时间更新为零
tomorrow := time.Now().AddDate(0, 0, 1) tomorrowZeroTime, err := time.Parse("Mon Jan 2 2006", tomorrow.Format("Mon Jan 2 2006")) if err != nil { // Handle Error } dueDate := time.Now() if dueDate.After(tomorrowZeroTime) { fmt.Println("Due date is after tomorrow") }
好了,本文到此结束,带大家了解了《如何检查rest api中的截止日期是否大于明天》,希望本文对你有所帮助!关注公众号,给大家分享更多Golang知识!