Go中获取当前日期信息和时间戳

时间:2025-03-02 08:15:24

1.获取当前时间

    currentTime:=()     //获取当前时间,类型是Go的时间类型Time
    t1:=().Year()        //年
    t2:=().Month()       //月
    t3:=().Day()         //日
    t4:=().Hour()        //小时
    t5:=().Minute()      //分钟
    t6:=().Second()      //秒
    t7:=().Nanosecond()  //纳秒
 
   currentTimeData:=(t1,t2,t3,t4,t5,t6,t7,) //获取当前时间,返回当前时间Time     
   (currentTime)       //打印结果:2017-04-11 12:52:52.794351777 +0800 CST
   (t1,t2,t3,t4,t5,t6)     //打印结果:2017 April 11 12 52 52
   (currentTimeData)    //打印结果:2017-04-11 12:52:52.794411287 +0800 CST

2.获取unix时间戳

now := ().Unix() //获取时间戳

3.转为UTC时间

().UTC() //转换为UTC时间