Golang time.Time.Nanosecond()函数用法和示例

Golang time.Time.Nanosecond()函数用法和示例

在Go语言中,time包提供了确定和查看时间的功能。Time.Nanosecond()函数在Go语言中用于查找“t”提供的秒内纳秒偏移量,范围为[0,999999999]。此外,该函数在time包中定义。因此你需要导入“time”包才能使用这些功能。

语法:

func (t Time) Nanosecond() int

这里,“t”是指定的时间。

返回值: 返回“t”所提供的秒内的纳秒偏移量。

示例 1:

// Golang program to illustrate the usage of
// Time.Nanosecond() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Declaring t in UTC
    t := time.Date(2017, 23, 5, 11,
              51, 04, 30, time.UTC)
  
    // Calling Nanosecond method
    nano := t.Nanosecond()
  
    // Prints nanoseconds as specified
    fmt.Printf("The stated nanoseconds "+
              "specified is: %v\n", nano)
} 

输出:

The stated nanoseconds specified is: 30

示例 2:

// Golang program to illustrate the usage of
// Time.Nanosecond() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Declaring t in UTC
    t := time.Date(2017, 34, 56, 78,
      87, 97, 687678678685757, time.UTC)
  
    // Calling Nanosecond method
    nano := t.Nanosecond()
  
    // Prints nanoseconds as specified
    fmt.Printf("The stated nanoseconds "+
              "specified is: %v\n", nano)
} 

输出:

The stated nanoseconds specified is: 678685757

这里,上述代码中指定的纳秒超出了通常范围,但在转换时被规范化。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程