Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

time.Time.Unix() Function in Golang with Examples

  • Last Updated : 21 Apr, 2020

In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Unix() function in Go language is used to yield “t” as a Unix time that is the number of seconds passed from January 1, 1970, in UTC and the output here doesn’t rely upon the location connected with t. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.

Syntax:

func (t Time) Unix() int64

Here, “t” is the stated time.

Note: A Unix like operating system frequently stores time as a 32-bit count of seconds. And on the other hand, the Unix() method here returns a 64-bit value so it’s period of validity is for billions of years into the past or the future.

Return value: It returns “t” as a Unix time which is of type int64.



Example 1:




// Golang program to illustrate the usage of
// Time.Unix() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining t in UTC for Unix method
    t := time.Date(2020, 11, 14, 11, 30, 32, 0, time.UTC)
  
    // Calling Unix method
    unix := t.Unix()
  
    // Prints output
    fmt.Printf("%v\n", unix)
}

Output:

1605353432

Example 2:




// Golang program to illustrate the usage of
// Time.Unix() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining t in UTC for Unix method
    t := time.Date(2013, 11, 14, 1e3, 3e5, 7e1, 0, time.UTC)
  
    // Calling Unix method
    unix := t.Unix()
  
    // Prints output
    fmt.Printf("%v\n", unix)
}

Output:

1405987270

Here, the time “t” stated in the above code has values which contain constant “e” which are converted in usual range while conversion.




My Personal Notes arrow_drop_up
Recommended Articles
Page :