Go的日期格式化


Go的日期格式,不同JavaShellPython
它提供一個特殊的字符串2006-01-02 15:04:05

示例代碼

package main

import (
    "fmt"
    "time"
)

//時間函數以及時間格式化

func main() {
    now := time.Now()
    //Year = now.Year()
    //Mouth  = now.Month()
    //Day  =  now.Day()
    //時間格式化輸出 Printf輸出
    fmt.Printf("當前時間為: %d-%d-%d %d:%d:%d\n", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
    //fmt.Sprintf 格式化輸出
    dateString := fmt.Sprintf("當前時間為: %d-%d-%d %d:%d:%d\n", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
    fmt.Println(dateString)
    //now.Format 方法格式化
    fmt.Println(now.Format("2006-01-02 15:04:05"))
    fmt.Println(now.Format("2006/01/02 15:04:05"))
    fmt.Println(now.Format("2006/01/02")) //年月日
    fmt.Println(now.Format("15:04:05"))   //時分秒

}

運行結果

當前時間為: 2021-11-2 15:15:18
當前時間為: 2021-11-2 15:15:18

2021-11-02 15:15:18
2021/11/02 15:15:18
2021/11/02
15:15:18


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM