Golang time.Now()函数及示例

Golang time.Now()函数及示例

在Go语言中,时间包提供确定和查看时间的功能。在Go语言中,“Now()”函数用于查找当前本地时间。此外,此函数在时间包中定义。在此,您需要导入“time”软件包才能使用这些功能。

语法:

func Now() Time
Go

返回值:返回当前本地时间。

示例1:

// Golang program to illustrate the usage of
// time.Now() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Calling Now() method
    tm := time.Now()
  
    // Prints current 
    // local time in UTC
    fmt.Printf("%s", tm)
} 
Go

输出:

2020-04-09 11:24:14.785868848 +0000 UTC m=+0.000187421
Go

在这里,当前时间以UTC返回。

示例2:

// Golang program to illustrate the usage of
// time.Now() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Calling Now() method
    tm := time.Now()
    tm1 := time.Now()
  
    // Prints current local time in UTC
    fmt.Printf("%s\n", tm)
  
    // Sleep for 10 seconds
    time.Sleep(10 * time.Second)
  
    // Prints the current time after 10
    // seconds of the sleep is over
    fmt.Printf("%s", tm1)
} 
Go

输出:

2020-04-09 11:37:59.087484568 +0000 UTC m=+0.000106559
2020-04-09 11:37:59.087484779 +0000 UTC m=+0.000106736
Go

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册