Home > Backend Development > Golang > How to Get the Unix Timestamp in Milliseconds Using Go?

How to Get the Unix Timestamp in Milliseconds Using Go?

Linda Hamilton
Release: 2024-12-15 17:06:14
Original
183 people have browsed it

How to Get the Unix Timestamp in Milliseconds Using Go?

Question: How can I obtain the Unix timestamp in milliseconds in Go?

Explanation:

The current Go function that retrieves the Unix timestamp, time.Now().UnixNano(), provides a value in nanoseconds. However, your requirement specifies converting this value to milliseconds.

Answer:

Currently, the time package in Go provides two convenient functions:

  • UnixMicro() for converting to microseconds
  • UnixMilli() for converting to milliseconds

Therefore, for Go versions 1.17 and later, the recommended solution is:

timestamp := time.Now().UnixMilli()
Copy after login

For Go versions 1.16 and earlier:

You can divide the nanosecond timestamp by 1e6, which represents the number of nanoseconds in a millisecond:

timestamp := time.Now().UnixNano() / 1e6
Copy after login

The above is the detailed content of How to Get the Unix Timestamp in Milliseconds Using Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template