[Golang] 输出格式化的时间,以及时间相关的一些方法

1. Go语言中设置时间格式比较特殊,需要按如下方式:
(1). time.Now().Format("2006-01-02 15:04:05"))

    输出为:2018-09-11 09:30:06
(2). time.Now().Format("06-1-2 3:4:5 PM"))

    输出为:18-9-11 9:30:6 AM

(3). 格式说明:

         月份 1,01,Jan,January
         日  2,02,_2 
         时  3,03,15,PM,pm,AM,am
         分  4,04
         秒  5,05
         年  06,2006
         时区 -07,-0700,Z0700,Z07:00,-07:00,MST
         周几 Mon,Monday

   

2. 当前日期直接输出: 

fmt.Printf("Now is %v\n", time.Now())
fmt.Printf("Now is %s\n", time.Now())

输出相同,为:Now is 2018-09-11 09:40:51.0622389 +0800 CST m=+0.041002301

 

3. 输出当前时间的时间戳:

长度为10的时间戳是以“秒”为单位; 
长度为13位数的时间戳是以“毫秒”为单位; 
长度为19位数的时间戳是以“纳秒”为单位;

fmt.Printf("Now is %v\n", time.Now().Unix())    //秒
fmt.Printf("Now is %v\n", time.Now().UnixNano())   //纳秒
fmt.Printf("Now is %v\n", time.Now().UnixNano()/1e6)  //纳秒转毫秒
fmt.Printf("Now is %v\n", time.Now().UnixNano()/1e9)   //纳秒转秒

输出:

Now is 1536631685
Now is 1536631685040620400
Now is 1536631685040
Now is 1536631685

猜你喜欢

转载自blog.csdn.net/youngwhz1/article/details/82620474