golang 日期信息和时间戳

时间:2025-03-02 08:13:26

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、时间戳

时间戳有10位、13位、还有好长位数的。
10位数的时间戳是以 秒 为单位;
13位数的时间戳是以 毫秒 为单位;
19位数的时间戳是以 纳秒 为单位;

golang中可以这样写:

package main

import (
    "time"
    "fmt"
)

func main() {
    ("时间戳(秒):%v;\n", ().Unix())
    ("时间戳(纳秒):%v;\n",().UnixNano())
    ("时间戳(毫秒):%v;\n",().UnixNano() / 1e6)
    ("时间戳(纳秒转换为秒):%v;\n",().UnixNano() / 1e9)
}

输出结果为:

时间戳(秒):1530027865;
时间戳(纳秒):1530027865231834600;
时间戳(毫秒):1530027865231;
时间戳(纳秒转换为秒):1530027865;