📜  如何在 Golang 中获取当前时间?

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

借助time.Now()函数,我们可以通过导入 time 模块来获取 Golang 中的当前时间。

示例#1:在这个示例中,我们可以看到通过使用time.Now()函数,我们能够获取当前日期和时间。

// Golang program to get the current time
package main
  
// Here "fmt" is formatted IO which
// is same as C’s printf and scanf.
import "fmt"
  
// importing time module
import "time"
  
// Main function
func main() {
  
    // Using time.Now() function.
    dt := time.Now()
    fmt.Println("Current date and time is: ", dt.String())
}

输出 :

Current date and time is:  2009-11-10 23:00:00 +0000 UTC m=+0.000000001

示例#2:

// Golang program to get the current time
package main
  
// Here "fmt" is formatted IO which
// is same as C’s printf and scanf.
import "fmt"
  
// importing time module
import "time"
  
// Main function
func main() {
  
    // Using time.Now() function.
    dt := time.Now()
    fmt.Println(dt.Format("01-02-2006 15:04:05"))
  
}

输出:

11-10-2009 23:00:00