// 1646789292945
    str := "2022-03-09T01:28:12.946770726Z"
    time1,_:=time.Parse(time.RFC3339Nano,str)
    fmt.Println(time1.UnixMilli())

    result, _ := time.ParseInLocation(time.RFC3339Nano, str, time.UTC)
    fmt.Println(result.UnixMilli())
}
func main() {
   //获取当前时间
   t := time.Now() //2018-07-11 15:07:51.8858085 +0800 CST m=+0.004000001
   fmt.Println(t)
 
   //获取当前时间戳
   fmt.Println(t.Unix()) //1531293019
 
   //获得当前的时间
   fmt.Println(t.Uninx().Format("2006-01-02 15:04:05"))  //2018-7-15 15:23:00
 
   //时间 to 时间戳
   loc, _ := time.LoadLocation("Asia/Shanghai")        //设置时区
   tt, _ := time.ParseInLocation("2006-01-02 15:04:05", "2018-07-11 15:07:51", loc) //2006-01-02 15:04:05是转换的格式如php的"Y-m-d H:i:s"
   fmt.Println(tt.Unix())                             //1531292871
 
   //时间戳 to 时间
   tm := time.Unix(1531293019, 0)
   fmt.Println(tm.Format("2006-01-02 15:04:05")) //2018-07-11 15:10:19
 
   //获取当前年月日,时分秒
   y := t.Year()                 //年
   m := t.Month()                //月
   d := t.Day()                  //日
   h := t.Hour()                 //小时
   i := t.Minute()               //分钟
   s := t.Second()               //秒
   fmt.Println(y, m, d, h, i, s) //2018 July 11 15 24 59


   datetime := "2015-01-01 00:00:00"  //待转化为时间戳的字符串

    //日期转化为时间戳
    timeLayout := "2006-01-02 15:04:05"  //转化所需模板  
    loc, _ := time.LoadLocation("Local")    //获取时区  
    tmp, _ := time.ParseInLocation(timeLayout, datetime, loc) 
    timestamp := tmp.Unix()    //转化为时间戳 类型是int64
    fmt.Println(timestamp) 

    //时间戳转化为日期  
    datetime = time.Unix(timestamp, 0).Format(timeLayout)
    fmt.Println(datetime)    
}

参考:http://www.zzvips.com/article...


goper
413 声望25 粉丝

go 后端开发