This is the 18th day of my participation in the August Genwen Challenge.More challenges in August
Year month day hour minute second
Now := time.now () FMT.Printf("%#v \n", Println(now.year ()) // Get the Month fmt.println (now.month ()) // get the Day fmt.println (now.day ()) // get the hour Fmt.println (now.hour ()) // Gets the Minute fmt.println (now.minute ()) // Gets the Second fmt.println (now.second ()) // gets the nanosecond fmt.Println(now.Nanosecond())Copy the code
Time formatting
In the corresponding time format string: himself month: 1:2:3 | 15 system using 12 hours 03, 24-hour use 15 points: 04 seconds: 05
Format(" January 2, 2006 03:04:05") fmt.Println(timeStr) // Parse the string to time timeStr2 := "2021-08-16 Loc, err := time.LoadLocation("Asia/Shanghai") if err! = nil { fmt.Println("parse in location failed: ", err) return} parseTime, err := time.ParseInLocation(layout, timeStr2, loc) ParseTime, err := time.Parse(Layout, timeStr2) if err! = nil { fmt.Println("time parse failed: ", err) } fmt.Println(parseTime)Copy the code
The time stamp
TimeUnix := now.unix () fmt.println (" Convert the time to a second timestamp: UnixNano := now.UnixNano() fmt.println (" Convert time to nanosecond timestamp := now. /* We specify a timestamp and convert it to the corresponding time */ t1 := time.Unix(timeUnix + int64(60 * 60), 0) fmt.println (t1)Copy the code
The time interval
The time interval in Go is a new type defined through INT64
type Duration int64
The value represents how many nanoseconds, and we can use the time unit indirectly using a defined time interval constant
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
Copy the code
Time.sleep (time.second * 1) // Millisecond (time.millisecond * 500)Copy the code
Operation of time
Println(" 时 代 时 : ", now) // add // 时 代 时 : ", now.add (time.hour * 1)) // 1 day after FMT.Println("1 day after: ", now.add (time.hour * 24)) // 1 Hour before: ", now.add (-time.Hour * 1)) // 1 day ago FMT.Println("1 day ago: ", now.Add(-time.Hour * 24)) // sub time1 := time.Now() time2 := time.Now().Add(-time.Hour * 10) subTime := time1.Sub(time2) fmt.Println(subTime.Hours()) fmt.Println(subTime.Minutes())Copy the code
The timer
Timer := time.tick (time.second) for I := range timer {fmt.println (I)}Copy the code
Constants in time
Month
/* Create a new type Month in Go. The value is 1 to 12 */ type Month int const (January Month = 1 + IOta February March April May June July August September October November December )Copy the code
Weekday
/* Define a new type Weekday by int, which is 0 on Sunday and 0 from Monday to Saturday: 1~6 */ type Weekday int const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday Saturday )Copy the code
conclusion
In the time package provides rich methods, we can easily get the current time year month day hour minute second, get the current time stamp, also can be very convenient to convert the timestamp to time. Provides the time operation method. You can also use the channel implementation timer to complete some scheduled tasks!!