https://play.golang.org/p/O1FWb9O97ld
func main() {
//const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t1, _ := time.Parse("03:04:05", "12:03:30")
fmt.Printf("current date time %v\n", t1)
now := time.Now().UTC()
t1.AddDate(now.Year(), int(now.Month()), now.Day())
fmt.Printf("current date time %v", t1)
}
输出
current date time 0000-01-01 12:03:30 +0000 UTC
current date time 0000-01-01 12:03:30 +0000 UTC
基本上我只解析时间但想要添加今天的日期。但是在调用time.AddDate()
后没有任何改变答案 0 :(得分:0)
AddDate
不会改变接收器的值(接收器不是指针)。您需要将值分配给某些内容,例如
t1 = t1.AddDate(now.Year(), int(now.Month())-1, now.Day()-1)
我还从1和1开始的月份和日期减1。
这可能是做你想做的更好的方法:
func main() {
//const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t1, _ := time.Parse("03:04:05", "12:03:30")
fmt.Printf("current date time %v\n", t1)
hour, min, sec := t1.Clock()
now := time.Now().UTC()
year, month, day := now.Date()
t1 = time.Date(year, month, day, hour, min, sec, 0, time.UTC)
fmt.Printf("current date time %v\n", t1)
}