SoFunction
Updated on 2025-03-05

golang implements the conversion of timestamps and timestamps

To be honest, the time conversion of golang is still very troublesome, at least it is much more troublesome than php. Please learn it carefully after adding the code.

package main 
import (
   "time"
   "fmt"
)
 
func main() {
   //Get the current time   t := () //2018-07-11 15:07:51.8858085 +0800 CST m=+0.004000001
   (t)
 
   //Get the current time stamp   (()) //1531293019
 
   //Get the current time   (().Format("2006-01-02 15:04:05"))  //2018-7-15 15:23:00
 
   //Time to Time Stamp   loc, _ := ("Asia/Shanghai")        //Set time zone   tt, _ := ("2006-01-02 15:04:05", "2018-07-11 15:07:51", loc) //2006-01-02 15:04:05 is the converted format such as "Y-m-d H:i:s" in php   (())                             //1531292871
 
   //Time stamp to time   tm := (1531293019, 0)
   (("2006-01-02 15:04:05")) //2018-07-11 15:10:19
 
   //Get the current year, month, day, hour, minute, and second   y := ()                 //Year   m := ()                //moon   d := ()                  //day   h := ()                 //Hour   i := ()               //minute   s := ()               //Second   (y, m, d, h, i, s) //2018 July 11 15 24 59
}

Additional: Golang 13-bit timestamp conversion

Timestamp:

Timestamps are data generated using digital signature technology. The signed objects include original file information, signature parameters, signature time and other information. The timestamp system is used to generate and manage timestamps, digitally sign the signed object to generate timestamps, to prove that the original file already existed before the signature time.

The difference between mysql type timestamp (timestamp) and datetime (time):

timestamp takes up 4 bytes;

datetime takes up 8 bytes;

timestamp range 1970-01-01 00:00:01.000000 to 2038-01-19 03:14:07.999999;

datetime is 1000-01-01 00:00:00.000000 to 9999-12-31 23:59:59.999999;

Timestamp format:

The 10-digit timestamp is in seconds, such as: 1530027865

The 13-digit timestamp is in milliseconds, such as: 1530027865231

The 19-digit timestamp is in nanoseconds, such as: 1530027865231834600

Golang gets the current time or time stamp

// string
().Format("2006-01-02 15:04:05")
// 
()
// int64 timestamp, default 10 digits, unit: seconds().UnixNano()

13-bit timestamp to time format

func UnixToTime(e string) (datatime , err error) {
 data, err := (e, 10, 64)
 datatime = (data/1000, 0)
 return
}

Time to 13-bit time stamp

func TimeToUnix(e ) int64 {
 timeUnix, _ := ("2006-01-02 15:04:05", ("2006-01-02 15:04:05"))
 return () / 1e6
}

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.