golang 时间与时间戳转换

golang 时间与时间戳转换

//golang 日期转时间戳
    a1 := "2022-01-11"
    a2 := "2022-02-22"

    MAT := "2006-01-02" //或 "2006-01-02 15:04:05" 固定格式固定内容
    x1, _ := time.ParseInLocation(MAT, a1, time.Local)
    x2, _ := time.ParseInLocation(MAT, a2, time.Local)

golang 时间戳转日期

//golang 时间戳转日期
    x1 := 1645459200
    x2 := 1645459200
    y1 := int(x1.Unix())
    y2 := int(x2.Unix())

    z1 := time.Unix(int64(1641830400), 0)
    z2 := time.Unix(int64(1645459200), 0)

    zz1 := z1.Format("2006-01-02") //或 "2006-01-02 15:04:05" 固定格式固定内容
    zz2 := z2.Format("2006-01-02") //或 "2006-01-02 15:04:05" 固定格式固定内容

golang 时间与时间戳转换

/todo :时间转时间戳

fmt.Println(time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local).Unix())

//todo :时间戳转时间

fmt.Println(time.Unix(1389058332, 0).Format(“2006-01-02 15:04:05”)) //2014-01-07 09:32:12

//"2006-1-2"  或 "2006-01-02 15:04:05" 固定格式固定内容 time.Local也是固定的
    MAT := "2006-1-2"// 固定格式
    Unix, _ := time.ParseInLocation(MAT, "2022-8-8", time.Local) //文本时间转成格式时间
    unixxx:=  Unix.Unix() //格式日期转时间戳

golang 生成时间

    yy := time.Now().Year()
    mm := int(time.Now().Month()) //特殊
    dd := time.Now().Day()
    
   currentTime := time.Now()                  //当前时间
   currentYear := time.Now().Year()        //当前年
   currentMonth := time.Now().Month()       //当前月
   currentDay := time.Now().Day()         //当前日
   currentHour := time.Now().Hour()        //当前小时小时
   currentMinute := time.Now().Minute()      //当前分钟
   currentSecond := time.Now().Second()      //当前秒
   currentNSecond := time.Now().Nanosecond()  //当前纳秒

golang 时间与时间戳转换

//golang 时间戳转日期
    x1 := 1645459200
    x2 := 1645459200
    y1 := int(x1.Unix())
    y2 := int(x2.Unix())

    z1 := time.Unix(int64(1641830400), 0)
    z2 := time.Unix(int64(1645459200), 0)

    zz1 := z1.Format("2006-01-02") //或 "2006-01-02 15:04:05" 固定格式固定内容
    zz2 := z2.Format("2006-01-02") //或 "2006-01-02 15:04:05" 固定格式固定内容
date := time.Now().Format("2006-01-02") //获取当前日期
    unix := time.Now().Unix()               //获取当前时间戳

644 Views
分享你的喜爱
linwute
linwute

我要像梦一样自由,像大地一样宽容;
在艰辛放逐的路上,点亮生命的光芒;
我要像梦一样自由,像天空一样坚强;
在曲折蜿蜒的路上,体验生命的意义;

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注