Golang gets the current time code

  • 2020-06-19 10:30:14
  • OfStack

Time dependent operations in golang are mainly functions of time package, and time mainly contains the object time.Time.

1. Get the current time

(1) currentTime:= time. Now() // gets the current time, of type Time

(2)

t1: = time Now (.) Year () / / year
t2: = time Now (.) Month () / / month
t3: = time Now (.) Day () / / day
t4: = time Now (.) Hour () / / hour
t5: = time Now (.) Minute () / / min
t6: = time Now (.) Second () / / SEC
t7: = time Now (.) Nanosecond () / / nanoseconds

currentTimeData: = time Date (t1 t2, t3, t4, t5, t6, t7, time. Local) / / get the current time, Time returns the current time

fmt.Println(currentTime) // Print result: 2017-04-11 12:52:52.794351777 +0800 CST
fmt. Println (t1 t2, t3, t4, t5, t6) / / print results: 52 April 11 12 2017 52
fmt.Println(currentTimeData) // Print: 2017-04-11 12:52:52.794411287 +0800 CST

now := time. Now().Unix() // Gets the timestamp
time.Now().UTC() // convert to UTC time
time.Sleep(10 * time.Millisecond) //time

Sample sharing:

Get current time

[

func getNow() {
// Gets the current time and returns the time.Time object
fmt.Println(time.Now())
// output: 2016-07-27 08:57:46.53277327 +0800 CST
// CST can be regarded as the standard time for America, Australia, Cuba or China
// +0800 means 8 hours faster than UTC time

// Gets the current timestamp
fmt.Println(time.Now().Unix())
// Accurate to the nanosecond, from which milliseconds and subtlety can be calculated
fmt.Println(time.Now().UnixNano())
// output:
// 1469581066
// 1469581438172080471
}

]

Format time display

[

func formatUnixTime() {
// Gets the current time and formats it
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
// output: 2016-07-27 08:57:46

// The specified time is formatted
fmt.Println(time.Unix(1469579899, 0).Format("2006-01-02 15:04:05"))
// output: 2016-07-27 08:38:19
}

]

Gets the year of the specified timestamp

[

func getYear() {
Gets the specified timestamp in hours, minutes and seconds
t := time.Unix(1469579899, 0)
fmt.Printf("%d-%d-%d %d:%d:%d\n", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
// output: 2016-7-27 8:38:19
}

]

Time string conversion timestamp

[

// Convert a time string such as 2016-07-27 08:46:15 into a timestamp
func strToUnix() {
// First, analyze the time string with ES194en. Parse. If correct, one time. Time object will be obtained
// This can be obtained using the time. Time object function Unix
t2, err := time.Parse("2006-01-02 15:04:05", "2016-07-27 08:46:15")
if err != nil {
log.Fatalln(err)
}
fmt.Println(t2)
fmt.Println(t2.Unix())
// output:
// 2016-07-27 08:46:15 +0000 UTC
// 1469609175
}

]

Gets the start of the day based on the timestamp

[

Gets the timestamp of the beginning of the day based on the timestamp
// This is often used in statistical functions
// The way to do this is to take a timestamp of 2016-01-01 00:00:00
// Then turn it into a timestamp OK
// Get month start time and year start time are similar
func getDayStartUnix() {
t := time.Unix(1469581066, 0).Format("2006-01-02")
sts, err := time.Parse("2006-01-02", t)
if err != nil {
log.Fatalln(err)
}
fmt.Println(sts.Unix())
// output: 1469577600
}

]

dormancy

[

/ / sleep
func sleep() {
// Sleep for 1 second
// time. Millisecond denotes 1 millisecond
// time.Microsecond means 1 subtlety
// time.Nanosecond means 1 nanosecond
time.Sleep(1 * time.Second)
// Sleep for 100 ms
time.Sleep(100 * time.Millisecond)

}

]

Related articles: