Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Last Updated : 24 Aug, 2021

In Go language, time packages supply functionality for determining as well as viewing time. The Time.UnixNano() 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) UnixNano() int64

Here, “t” is the stated time.
Note: Here, the output returned is not defined if the given Unix time in nanoseconds is not formed by an int64 type(which is a date before the year 1678 or after the year 2262). This implies that the result of calling the UnixNano() method on the zero Time is ambiguous.
Return value: It returns “t” as a Unix time which is of type int64. 

Example 1:

Go






// Golang program to illustrate the usage of
// Time.UnixNano() function
 
// Including main package
package main
 
// Importing fmt and time
import "fmt"
import "time"
 
// Calling main
func main() {
 
    // Defining t in UTC
    // for UnixNano method
    t := time.Date(2019, 13, 15, 23,
               90, 12, 04, time.UTC)
 
    // Calling UnixNano method
    unixnano := t.UnixNano()
 
    // Prints output
    fmt.Printf("%v\n", unixnano)
}

Output: 

1579134612000000004

Example 2:

Go




// Golang program to illustrate the usage of
// Time.UnixNano() function
 
// Including main package
package main
 
// Importing fmt and time
import "fmt"
import "time"
 
// Calling main
func main() {
 
    // Defining t in UTC
    // for UnixNano method
    t := time.Date(2001, 13, 15, 2e3,
           1e1, 12e2, 04e1, time.UTC)
 
    // Calling UnixNano method
    unixnano := t.UnixNano()
 
    // Prints output
    fmt.Printf("%v\n", unixnano)
}

Output: 

input
1018254600000000040

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :