Go基础-时间和日期函数

package main

import (
    "fmt"
    "time"
)

func main() {

    // 获取当前时间
    now := time.Now()
    fmt.Printf("now %v \n type %t\n", now, now)

    // 通过now获取年月日时分秒
    fmt.Printf("年=%v\n", now.Year())
    fmt.Printf("月=%v\n", now.Month())
    fmt.Printf("月=%v\n", int(now.Month()))
    fmt.Printf("日=%v\n", now.Day())
    fmt.Printf("时=%v\n", now.Hour())
    fmt.Printf("分=%v\n", now.Minute())
    fmt.Printf("秒=%v\n", now.Second())

    // 通过time.Formant() 方法获取 2006-01-02 15:04:05 是固定的
    fmt.Println(now.Format("20060102150405"))
    fmt.Println(now.Format("2006-01-02"))
    fmt.Println(now.Format("15:04:05"))
    fmt.Println(now.Format("01"))

    // Unix 和 UnixNano的使用
    fmt.Printf("Unix时间戳%v,UnixNano 纳秒%v", now.Unix(), now.UnixNano())

}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 2

可以看看这个时间处理库 github.com/golang-module/carbon

2年前 评论
rockygao (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!