Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Timestamp of UTC time with Golang?

I want to convert UTC time string to unix timestamp. I do this

fmt.Printf("%s %d\n", time.Now().String(), time.Now().Unix()) fmt.Printf("%s %s\n", time.Now().UTC().String(), time.Now().UTC().Unix()) 

But I got same unix timestamp 1499018765

2017-07-02 20:06:05.5582802 +0200 CEST 1499018765

2017-07-02 18:06:05.791337 +0000 UTC 1499018765

like image 368
LeMoussel Avatar asked Jul 02 '17 18:07

LeMoussel


People also ask

How do I get UTC timestamp?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation.

What is UTC Golang?

UTC() function in Go language is used to yield “t” with the location that is set to UTC. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.

How do I get the timestamp on go?

You can use time. Now(). UTC() if you'd rather have UTC than your local time zone.

What is the UTC timestamp?

The abbreviation UTC stands for Coordinated Universal Time or Temps Universal Coordonné and denotes the time, derived from atomic time, valid all over the world. This uses the 24-hour clock and the Gregorian Calendar as its standard. Note that this time always relates to Greenwich Mean Time, or GMT.


1 Answers

Unix() always returns the number of seconds elapsed since January 1, 1970 UTC. So it does not matter whether you give it time.Now() or time.Now().UTC(), it is the same UTC time, just in different places on Earth. What you get as the result is correct.

like image 74
Constantin S. Pan Avatar answered Sep 22 '22 02:09

Constantin S. Pan