go time用法

常用命令

工作中常见问题

1、字符串时间戳转成时间格式

     //字符串时间戳转成时间表格
    time_string:=1517367099
    tm:=time.Unix(int64(time_string),0).Format("2006-01-02") //Format 可以指定要显示的格式
    fmt.Println("时间=",tm)

2、获取当前时间戳

    //获取当前时间戳
    tm:=time.Now().Unix()
    fmt.Println("时间=",tm)
3、字符串转成时间戳

    //字符串转成时间戳
    time_string:="2018-01-31 10:51:39"
    timeLayout := "2006-01-02 15:04:05"  //转化所需模板
    loc, _ := time.LoadLocation("Local") //获取时区
    tm, _ := time.ParseInLocation(timeLayout, time_string, loc)
    fmt.Println("时间=",tm.Unix())
4、获取当地时间

    //获取当地时间
    tm:=time.Now().Local()
    fmt.Println("时间=",tm)




你可能感兴趣的:(Go)