Go 语言时间和日期函数的使用
时间和日期相关函数,需导入 time 包
。
1.获取当前时间:
package main
import (
"fmt"
"time"
)
func main() {
// 获取当前时间
now := time.Now()
fmt.Printf("当前时间为:%v\n类型为:%T", now, now)
}
// 输出结果:
当前时间为:2020-01-20 11:38:23.678291 +0800 CST m=+0.002991101
类型为:time.Time
2.获取日期等相关信息(年、月、日、时、分、秒):
package main
import (
"fmt"
"time"
)
func main() {
//获取当前时间
now := time.Now()
fmt.Printf("当前时间为:%v\n类型为:%T", now, now)
// 通过now 可以获取到 年、月、日、时、分、秒等信息
// 获取年
nowYear := now.Year()
fmt.Printf("年=%v\n", nowYear)
// 获取月份
nowMonth := now.Month()
fmt.Printf("月=%v\n", nowMonth)
// 将月份转换成数字 1~12 数字显示
fmt.Printf("%v月份\n", int(nowMonth))
// 获取日期
nowDay := now.Day()
fmt.Printf("日期=%v\n", nowDay)
// 获取小时
nowHour := now.Hour()
fmt.Printf("时=%v\n", nowHour)
// 获取分钟
nowMinute := now.Minute()
fmt.Printf("分钟=%v\n", nowMinute)
// 获取秒数
nowSecond := now.Second()
fmt.Printf("秒数=%v\n", nowSecond)
}
// 输出如下:
当前时间为:2020-01-20 11:47:02.956236 +0800 CST m=+0.002990501
类型为:time.Time年=2020
月=January
1月份
日期=20
时=11
分钟=47
秒数=2
3.格式化日期时间:
-
使用Printf 或 SPrintf:
package main import ( "fmt" "time" ) func main() { //获取当前时间 now := time.Now() fmt.Printf("当前时间为:%v\n类型为:%T\n", now, now) // 使用Printf 进行格式化 fmt.Printf("当前年月日的时间为: %d-%d-%d %d:%d:%d\n", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) // 使用Sprintf 将返回值赋值给一个变量, 进行格式化输出 dateStr := fmt.Sprintf("%d-%d-%d %d:%d:%d\n", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) fmt.Printf("当前年月日的时间为: %v", dateStr) } // 输出如下: 当前时间为:2020-01-20 11:58:25.2377586 +0800 CST m=+0.001995201 类型为:time.Time 当前年月日的时间为: 2020-1-20 11:58:25 当前年月日的时间为: 2020-1-20 11:58:25
-
使用time.Format()方法:
package main import ( "fmt" "time" ) func main() { //获取当前时间 now := time.Now() fmt.Printf("当前时间为:%v\n类型为:%T\n", now, now) // 使用time.Format()方法完成 fmt.Printf(now.Format("2006/01/02 15:04:05")) fmt.Println() fmt.Printf(now.Format("2006-01-02")) fmt.Println() fmt.Printf(now.Format("15:04:05")) } // 输出结果如下: 当前时间为:2020-01-20 14:07:10.5955972 +0800 CST m=+0.001999601 类型为:time.Time 2020/01/20 14:07:10 2020-01-20 14:07:10
注意:为什么是2006-01-02 15:04:05?:
-
2006-01-02 15:04:05
这是固定写法,类似其它语言Y-m-d H:i:s
写法,为什么采用这种形式?为什么是这个时间点,而不是其它时间点呢?官方说,这个时间点,比较好记。
2006年1月2日3点4分5秒
-
4.时间的常量:
const (
Nanosecond Duration = 1 // 纳秒
Microsecond = 1000 * Nanosecond //微妙
Millisecond = 1000 * Microsecond // 毫秒
Second = 1000 * Millisecond // 秒
Minute = 60 * Second // 分钟
Hour = 60 * Minute // 小时
)
-
结合Sleep来使用一下时间常量:
package main import ( "fmt" "time" ) func main() { // 使用时间的常量, 每隔1秒打印一个数字, 打印到100的时候退出 i := 0 for { i++ time.Sleep(time.Millisecond * 1000) // 1秒钟 if i == 100 { break } fmt.Println("i:", i) } }
5.时间戳:
-
Unix:从1970年1月1日时间点开始计算的时间(单位:秒);
-
UnixNano:从1970年1月1日时间点开始计算的时间(单位:纳秒);
如果纳秒为单位unix时间超出了 int64 表示的范围,结果是未定义
package main
import (
"fmt"
"time"
)
func main() {
// 秒、纳秒 时间戳的使用
now := time.Now()
fmt.Printf("时间为:%v\n", now)
fmt.Printf(now.Format("2006-01-02 15:04:05"))
fmt.Println()
fmt.Printf("unix时间戳(单位:秒)=%v\n", now.Unix())
fmt.Printf("unix时间戳(单位:纳秒)=%v", now.UnixNano())
}
// 输出结果:
时间为:2020-01-20 14:48:41.988597 +0800 CST m=+0.002990001
2020-01-20 14:48:41
unix时间戳(单位:秒)=1579502921
unix时间戳(单位:纳秒)=1579502921988597000