英文:
Compare time in Go
问题
我有一个包含列created_at
的表,我需要将该列中的时间与当前时间进行比较,例如:
result := database.DB.Where("code = ?", post.code).First(&post)
if result.CreatedAt.Before(time.Now()) {
// 进行某些操作
}
我在编辑器中遇到错误:Invalid operation: result.CreatedAt > time.Now() (the operator > is not defined on Time)
(无效操作:result.CreatedAt > time.Now()(Time类型上未定义操作符>))
如何检查日期是否过期?
英文:
I have a table contains column created_at
I need to compare the time in this column with the now time for example:
result := database.DB.Where("code = ?", post.code).First(&post)
if result.CreatedAt < time.Now() {
// do something
}
I got error in my editor: Invalid operation: result.CreatedAt > time.Now() (the operator > is not defined on Time)
How can check if the date expires?
答案1
得分: 3
使用time.After或time.Before函数。
package main
import (
"fmt"
"time"
)
func main() {
created := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
now := time.Now()
if created.Before(now) {
fmt.Println("做一些事情")
}
}
英文:
By using time.After or time.Before.
package main
import (
"fmt"
"time"
)
func main() {
created := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
now := time.Now()
if created.Before(now) {
fmt.Println("do something")
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论