代码示例
package main import ( "fmt" "os" "time" ) //time包练习 func main() { timeone := "" //当前时间 now := time.Now() time1 := now.Format("2006-01-02 15:04:05") fmt.Println(time1) // 年月日 year := now.Year() month := now.Month() day := now.Day() hour := now.Hour() minute := now.Minute() second := now.Second() time2 := fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second) fmt.Println(time2) //当前时间的前一天 yesterday := time.Now().Add(time.Hour*-24).Format("2006-01-02 15:04:05") fmt.Println(yesterday) // 当前时间的第二天 afterDay := time.Now().Add(time.Hour*24).Format("2006-01-02 15:04:05") fmt.Println(afterDay) //fmt.Println(len(os.Args),os.Args) if len(os.Args) == 1{ timeone = yesterday }else{ timeone = os.Args[1] } fmt.Println(timeone) // 定时器 // ticker := time.Tick(time.Second) // for i := range ticker{ // fmt.Println(i.Format("2006-01-02 15:04:05")) // } // timer timer := time.NewTimer(time.Second*2) t1 := time.Now() t2 := <-timer.C fmt.Println(t1.Sub(t2)) timer2 := time.NewTimer(time.Second*10) timer2.Reset(time.Second*4) t3 := time.Now() fmt.Println(t3) t4 := <-timer2.C timer2.Stop() fmt.Println(t4) }
原文:https://www.cnblogs.com/pebblecome/p/14312637.html