Get Unix Time in Milliseconds with Go's time.Now().UnixNano()
In Go, the time.Now().UnixNano() function provides a high-precision timestamp in nanosecond resolution. To obtain milliseconds instead, a simple conversion is necessary.
For Go v1.17 and Later:
In Go versions 1.17 and above, the time package conveniently introduced the UnixMicro() and UnixMilli() methods. To get the timestamp in milliseconds:
milliseconds := time.Now().UnixMilli()
For Go v1.16 and Earlier:
For earlier Go versions, division by 1e6 (1 million) can be used to convert from nanoseconds to milliseconds:
milliseconds := time.Now().UnixNano() / 1e6
For example:
import ( "time" "fmt" ) func main() { ms := time.Now().UnixNano() / 1e6 fmt.Println(ms) // Output: 1665376382059 }
The above is the detailed content of How Do I Get the Current Unix Time in Milliseconds Using Go?. For more information, please follow other related articles on the PHP Chinese website!