Golang time.Milliseconds() 函数及示例

Golang time.Milliseconds() 函数及示例

Go 语言中的 time 包提供计算和查看时间的功能。在 Go 语言中, Milliseconds() 函数用于以整数毫秒计算的形式查找时间的持续时间。此外,此函数在 time 包下定义。在此,需要导入“time”包才能使用这些函数。

语法:

func (d Duration) Milliseconds() int64

这里的 d 是时间的持续时间。

返回值:它作为 int64 返回持续时间值。

示例 1:

// Golang program to illustrate the usage of
// Milliseconds() function
  
// Including main package
package main
  
// Importing fmt and time
import (
    "fmt"
    "time"
)
  
// Calling main
func main() {
  
    // Defining duration
    // of Milliseconds method
    milli, _ := time.ParseDuration("7s")
  
    // Prints duration as 
    // an integer milliseconds
    fmt.Printf("Only %d milliseconds of "+
      "task is remaining.", milli.Milliseconds())
} 

输出:

Only 7000 milliseconds of task is remaining.

示例 2:

// Golang program to illustrate the usage of
// Milliseconds() function
  
// Including main package
package main
  
// Importing fmt and time
import (
    "fmt"
    "time"
)
  
// Calling main
func main() {
  
    // Defining duration of 
    // Milliseconds method
    milli, _ := time.ParseDuration("2h1m2s4545ns")
  
    // Prints duration as 
    // an integer milliseconds
    fmt.Printf("Only %d milliseconds of"+
     " task is remaining.", milli.Milliseconds())
} 

输出:

Only 7262000 milliseconds of task is remaining.

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程