关于go:go-时间字符串转标准时间

11次阅读

共计 635 个字符,预计需要花费 2 分钟才能阅读完成。

package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "2006-01-02 15:04:05"
    str := "2022-09-05T12:03:15Z"
    t := DateTimeStringFormat(str, layout)
    fmt.Println(t)
}

// DateTimeStringFormat 工夫格式化  字符串格式化 ---- 后果字符串
func DateTimeStringFormat(timeValue string, fmt string) string {
    timeLayout := "2006-01-02T15:04:05.999999999Z07:00"            // 所需模板
    loc, _ := time.LoadLocation("Local")                           //*** 获取时区 ***
    theTime, _ := time.ParseInLocation(timeLayout, timeValue, loc) // 应用模板在对应时区转化为 time.time 类型

    // 0001-01-01T00:00:00Z 这里也示意工夫为 null
    if theTime.IsZero() {return ""} else {
        // 工夫戳转日期
        //dataTimeStr := theTime.Format("2006-01-02 15:04:05") // 应用模板格式化为日期字符串
        dataTimeStr := theTime.Format(fmt) // 应用模板格式化为日期字符串
        return dataTimeStr
    }
}

正文完
 0