go內置包time:time.Now()格式化

1. 輸出當前日期和時間

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()

    fmt.Println(now)
    
    // 必須使用這個時間,才能返回正確的格式化後的當前時間,其他的都不行
    fmt.Println(now.Format("2006-01-02 15:04:05"))
    fmt.Println(now.Format("2006/1/2 15:04:05"))
    fmt.Println(now.Format("2006/01/02 15:04:05"))
    fmt.Println(now.Format("15:04:05 2006/1/2"))
    fmt.Println(now.Format("2006/1/2"))
}

打印結果:

2019-10-29 16:47:38.2915991 +0800 CST m=+0.002990101
2019-10-29 16:47:38
2019/10/29 16:47:38
2019/10/29 16:47:38
16:47:38 2019/10/29
2019/10/29

2. 參考文章

https://blog.csdn.net/youngwhz1/article/details/82620474

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章