📜  Golang 中的 time.Time.Second()函数示例

📅  最后修改于: 2021-10-25 02:33:27             🧑  作者: Mango

在 Go 语言中,时间包提供了确定和查看时间的功能。 Go 语言中的Time.Second()函数用于查找“t”提供的分钟内的第二个偏移量,范围为 [0, 59]。而且,这个函数是在time包下定义的。在这里,您需要导入“time”包才能使用这些功能。

句法:

func (t Time) Second() int

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

返回值:它返回“t”提供的分钟内的第二个偏移量。

示例 1:

// Golang program to illustrate the usage of
// Time.Second() 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, 2, 3, 13, 
             12, 34, 50, time.UTC)
  
    // Calling Second method
    sec := t.Second()
  
    // Prints second as specified
    fmt.Printf("The stated second is: %v\n", sec)
}

输出:

The stated second is: 34

示例 2:

// Golang program to illustrate the usage of
// Time.Second() 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, 2, 3, 13,
             12, 67, 50, time.UTC)
  
    // Calling Second method
    sec := t.Second()
  
    // Prints second as specified
    fmt.Printf("The stated second is: %v\n", sec)
}

输出:

The stated second is: 7

此处,上述代码中所述的秒数超出了通常的范围,但在转换时对其进行了标准化。