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

📅  最后修改于: 2021-10-24 13:31:20             🧑  作者: Mango

在 Go 语言中,时间包提供了确定和查看时间的功能。 Go 语言中的Time.Date()函数用于检查所述“t”出现的年、月和日。而且,这个函数是在time包下定义的。在这里,您需要导入“time”包才能使用这些功能。

句法:

func (t Time) Date() (year int, month Month, day int)

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

返回值:返回指定“t”的年、月、日。

示例 1:

// Golang program to illustrate the usage of
// Time.Before() 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(2020, 5, 6, 11, 45, 04, 0, time.UTC)
  
    // Calling Date method
    yyyy, mm, dd := t.Date()
  
    // Prints year
    fmt.Printf("The stated year is: %v\n", yyyy)
  
    // Prints month
    fmt.Printf("The stated month is: %v\n", mm)
  
    // Prints day
    fmt.Printf("The stated day is: %v\n", dd)
}

输出:

The stated year is: 2020
The stated month is: May
The stated day is: 6

示例 2:

// Golang program to illustrate the usage of
// Time.Before() 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(2020, 13, 34, 00, 00, 00, 0, time.UTC)
  
    // Calling Date method
    yyyy, mm, dd := t.Date()
  
    // Prints year
    fmt.Printf("The stated year is: %v\n", yyyy)
  
    // Prints month
    fmt.Printf("The stated month is: %v\n", mm)
  
    // Prints day
    fmt.Printf("The stated day is: %v\n", dd)
}

输出:

The stated year is: 2021
The stated month is: February
The stated day is: 3

在这里,规定的月份和日期超出了通常的范围,但它们在转换时被标准化。