在 Golang 中,可以使用 time.Time 类型的 Year()Month()Day() 方法来获取年、月和日。

例如:

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    year, month, day := t.Date()
    fmt.Printf("今天是 %d 年 %d 月 %d 日\n", year, month, day)
}

输出:

今天是 2022 年 2 月 27 日

其中,time.Now() 函数获取当前时间,t.Date() 方法获取时间 t 的年、月、日。可以使用 Printf() 函数将结果格式化为字符串。

Golang 时间获取: 如何使用 time.Time 获取年月日

原文地址: http://www.cveoy.top/t/topic/lHpp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录