Go基礎-時間和日期函式

rockygao發表於2022-02-22
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 協議》,轉載必須註明作者和本文連結

相關文章