Golang time.Time.Date()函数及示例
在Go语言中,time包提供了确定和查看时间的功能。在Go语言中, Time.Date() 函数用于检查所述“t”出现的年,月和日。此外,该函数在time包下定义。在这里,您需要导入“time”包以使用这些函数。
语法:
func (t Time) Date() (year int, month Month, day int)
此处,“t”是指定的时间。
返回值: 它返回所述“t”的年,月和日。
例1:
// Golang程序演示
// Time.Before()函数的用法
// 包括主包
package main
// 导入fmt和time
import "fmt"
import "time"
// 调用主要函数
func main() {
// 在UTC中声明t
t := time.Date(2020, 5, 6, 11, 45, 04, 0, time.UTC)
// 调用日期方法
yyyy,mm,dd := t.Date()
// 打印年份
fmt.Printf("所述年份为:%v\n",yyyy)
// 打印月份
fmt.Printf("所述月份为:%v\n",mm)
// 打印日份
fmt.Printf("所述日期为:%v\n",dd)
}
输出:
所述年份为:2020
所述月份为:May
所述日期为:6
例2:
// Golang程序演示
// Time.Before()函数的用法
// 包括主包
package main
// 导入fmt和time
import "fmt"
import "time"
// 调用主要函数
func main() {
// 在UTC中声明t
t := time.Date(2020, 13, 34, 00, 00, 00, 0, time.UTC)
// 调用日期方法
yyyy,mm,dd := t.Date()
// 打印年份
fmt.Printf("所述年份为:%v\n",yyyy)
// 打印月份
fmt.Printf("所述月份为:%v\n",mm)
// 打印日份
fmt.Printf("所述日期为:%v\n",dd)
}
输出:
所述年份为:2021
所述月份为:February
所述日期为:3
在这里,所述月份和日期超出了通常的范围,但是它们在转换时被标准化了。