SoFunction
Updated on 2025-03-03

Golang's time package: seconds, milliseconds, nanosecond timestamp output method

When I was a rookie, I only knew that the timestamps had 10 digits, 13 digits, and a lot of long digits.

I only realized after a long time

The 10-digit timestamp is in seconds;

The 13-digit timestamp is in milliseconds;

The 19-digit timestamp is in nanoseconds;

You can write this in golang:

package main
import (
 "time"
 "fmt"
)
func main() {
 ("Timestamp (seconds): %v;\n", ().Unix())
 ("Timestamp (nanosecond): %v;\n",().UnixNano())
 ("Timestamp (milliseconds): %v;\n",().UnixNano() / 1e6)
 ("Timestamp (nanoseconds converted to seconds): %v;\n",().UnixNano() / 1e9)
}

The output result is:

Timestamp (seconds): 1530027865;

Timestamp (nanosecond): 1530027865231834600;

Timestamp (milliseconds): 1530027865231;

Timestamp (nanoseconds converted to seconds): 1530027865;

Supplement: golang gets the current time, timestamp and time string and their mutual conversions

1. Get the current time

(1)

currentTime:=() //Get the current time, the time type of Go is Time

(2)

t1:=().Year() //Yeart2:=().Month() //moont3:=().Day()  //dayt4:=().Hour() //Hourt5:=().Minute() //minutet6:=().Second() //Secondt7:=().Nanosecond() //Nanosec 
currentTimeData:=(t1,t2,t3,t4,t5,t6,t7,) //Get the current time and return the current time Time 
(currentTime) //Print result: 2017-04-11 12:52:52.794351777 +0800 CST(t1,t2,t3,t4,t5,t6) //Print result: 2017 April 11 12 52 52(currentTimeData) //Print result: 2017-04-11 12:52:52.794411287 +0800 CST

Note: From the printing results, we can see that both the () and Date() methods can obtain the current time. () is relatively simple to use, but Date() can obtain different exact values. For example, (t1, t2, t3, t4, t5, t6, 0,) will omit milliseconds, accurate to seconds, the result is: 2017-04-11 12:52:52 +0800 CST

2. Get the current time stamp

timeUnix:=().Unix() //Units s, print result: 1491888244

timeUnixNano:=().UnixNano() //Unit nanoseconds, print result: 1491888244752784461

3. Get the string format of the current time

timeStr:=().Format("2006-01-02 15:04:05") //The string of the current time, 2006-01-02 15:04:05 is said to be the birth time of golang, fixed writing method

(timeStr) //Print result: 2017-04-11 13:24:04

4. The mutual transformation between them

1) Timestamp to time string (int64 —> string)

timeUnix:=().Unix() // Known timestampformatTimeStr:=(timeUnix,0).Format("2006-01-02 15:04:05")
(formatTimeStr) //Print results:2017-04-11 13:30:39

2) Time string to time (string —> Time)

formatTimeStr=”2017-04-11 13:33:37”
formatTime,err:=("2006-01-02 15:04:05",formatTimeStr)
if err==nil{
 (formatTime) //Print result: 2017-04-11 13:33:37 +0000 UTC}

3) Time string to timestamp (string —> int64)

One more step than above, just ()

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.