美文网首页GoGo语言实践Go
goland 时间戳和字符串之间的转换

goland 时间戳和字符串之间的转换

作者: wuhan_goer | 来源:发表于2020-04-19 10:36 被阅读0次

    1.时间戳转成字符串

    func main() {
    
    // 当前时间戳转字符串日期
    //1. 生成Time对象time.Now()或time.Unix()这俩个方法会返回Time
    //2.Format方法会返回字符串
      timeStr :=  time.Now().Format("2006-01-02 15:04:05")
    
    fmt.Println("时间戳转日期",timeStr)
    
    // 指定时间戳转字符串日期
    
      var t int64  =1587030950
    
      timeObj  := time.Unix(t,0).Format("2006-01-02 15:04:05")
    
    fmt.Println("指定时间戳转换成日期",timeObj)
    
    }
    
    image.png
    func main()  {
            // 字符串日期转时间戳,值得注意的是,这里strTime的格式必须与下面Parse 的layout的参数格式一样.
            strTime := "2020-04-17 00:00:00"
            //设置时区
            loc, _ := time.LoadLocation("Asia/Shanghai")
            t ,err:=time.ParseInLocation("2006-01-02 15:04:05",strTime,loc)
            if err != nil {
                fmt.Println("err is ",err)
                return
            }
            fmt.Println("字符串日期转时间戳",t.Unix())
    
    }
    
    image.png

    相关文章

      网友评论

        本文标题:goland 时间戳和字符串之间的转换

        本文链接:https://www.haomeiwen.com/subject/zuzyvhtx.html