package main
import (
"fmt"
"time"
)
func testTime() {
now := time.Now()
fmt.Printf("current time: %v\n ", now)
year := now.Year()
month := now.Month()
day := now.Day()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
fmt.Printf("时间字符串%05d-%02d-%02d %02d:%02d:%02d\n ", year, month, day, hour, minute, second)
fmt.Printf("类型%T,%T,%T %T,%T,%T\n ", year, month, day, hour, minute, second)
// 上面的%05d表示填充符号,不足5位用0填充,满足了就不管了了
timestamp := now.Unix()
fmt.Printf("timestamp is:%d\n", timestamp)
}
// # 时间戳,从1970年到现在的时间的秒数,时间戳数字转化为时间格式
func testTimestamp(timestamp int64) {
timeObj := time.Unix(timestamp, 0)
year := timeObj.Year()
month := timeObj.Month()
day := timeObj.Day()
hour := timeObj.Hour()
minute := timeObj.Minute()
second := timeObj.Second()
fmt.Printf("current timestamp :%d\n", timestamp)
fmt.Printf("%02d-%02d-%02d %02d:%02d:%02d \n ", year, month, day, hour, minute, second)
}
func processTask() {
fmt.Printf("do task\n")
}
// 定时器
func testTicker() {
ticker := time.Tick(5 * time.Second) //每1秒,会把当前时间放入定时器管道里面
for i := range ticker {
fmt.Printf("这是i: %v\n", i)
processTask()
}
}
func testConst() {
fmt.Printf("nano second :%d\n", time.Nanosecond) // 纳秒
fmt.Printf("micro second :%d\n", time.Microsecond) // 微秒
fmt.Printf("milli second :%d\n", time.Millisecond) // 毫秒
fmt.Printf("second :%d\n", time.Second) // 秒
}
func testFormat() {
now := time.Now()
timeStr := now.Format("2006-01-02 15:04:05") // 用format格式化时间,年月日必须是2016年,1月2日,go语言诞生的时间,以这个2006-01-02 15:04:05时间作为模板
fmt.Printf("01>>time :%s\n", timeStr)
timeStr = now.Format("2006-1-2 15:04:05") // 用format格式化时间,年月日必须是2016年,1月2日,go语言诞生的时间
fmt.Printf("02>>time :%s\n", timeStr)
timeStr = now.Format("2006/01/02 15:04:05") // 用format格式化时间,年月日必须是2016年,1月2日,go语言诞生的时间
fmt.Printf("03>>time :%s\n", timeStr)
timeStr = now.Format("01/02/2006 15:04:05") // 用format格式化时间,年月日必须是2016年,1月2日,go语言诞生的时间
fmt.Printf("04>>time :%s\n", timeStr)
timeStr = now.Format("01/02/2006 3:04:05") // 用format格式化时间,下午3点,12小时的格式
fmt.Printf("05>>time :%s\n", timeStr)
}
func testFormat2() {
now := time.Now()
timeStr := fmt.Sprintf("%02d-%02d-%02d %02d:%02d:%02d \n ",
now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
fmt.Printf("time :%s\n", timeStr)
}
func testCost() {
start := time.Now().UnixNano() // 纳秒
for i := 0; i < 10; i++ {
time.Sleep(time.Millisecond)
}
end := time.Now().UnixNano()
cost := (end - start) / 1000
fmt.Printf("code cost:%d us\n", cost) //code cost:10000 us 大概10毫秒左右
}
func main() {
//testTime()
//timestamp := time.Now().Unix()
//testTimestamp(timestamp)
//testTicker() //定时器
//testConst()
testFormat()
testFormat2()
testCost()
}
输出:
01>>time :2022-03-12 18:29:36
02>>time :2022-3-12 18:29:36
03>>time :2022/03/12 18:29:36
04>>time :03/12/2022 18:29:36
05>>time :03/12/2022 6:29:36
time :2022-03-12 18:29:36
code cost:10000 us
案例二:
package main
import (
"fmt"
"time"
)
const (
//time format
FormatDay = "2006-01-02"
FormatSecond = "2006-01-02 15:04:05"
FormatMinute = "2006-01-02 15:04"
FormatOnlyHour = "15:04"
)
func main() {
fmt.Printf("nano second :%d\n", time.Nanosecond) // 纳秒
fmt.Printf("micro second :%d\n", time.Microsecond) // 微秒
fmt.Printf("milli second :%d\n", time.Millisecond) // 毫秒
fmt.Printf("second :%d\n", time.Second) // 秒
//获取月
fmt.Println(time.Month(1))
//当前时间
fmt.Println(time.Now())
fmt.Println(time.Now().String())
//当前时间-时
fmt.Println(time.Now().Hour())
//当前时间unix时间戳since 1970-1-1
fmt.Println(time.Now().Unix())
//当前时间unix时间戳(nanoseconds),since 1970 -1- 1,
fmt.Println(time.Now().UnixNano())
//当前时间加三个小时
fmt.Println(time.Now().Add(time.Hour * 3))
//时间戳转化成时间
currentTime := time.Now().Unix()
tm := time.Unix(currentTime, 0)
fmt.Println(tm)
//当前时间戳转成日期
fmt.Println(time.Now().Format(FormatDay))
fmt.Println(time.Now().Format(FormatSecond))
fmt.Println(time.Now().Format(FormatMinute))
fmt.Println(time.Now().Format(FormatOnlyHour))
//日期转成时间戳
timestamp, _ := time.Parse(FormatDay, "2019-12-13")
fmt.Println(timestamp) //标准时间格式
fmt.Println(timestamp.Unix()) //时间戳
//时区修改后获取时间戳;
loc, _ := time.LoadLocation("Asia/Shanghai")
t, _ := time.ParseInLocation(FormatDay, "2019-12-13", loc)
fmt.Println(t) //标准时间格式
fmt.Println(t.Unix()) //时间戳
}
输出:
nano second :1
micro second :1000
milli second :1000000
second :1000000000
January
2022-04-30 17:30:49.7776039 +0800 CST m=+0.011000001
2022-04-30 17:30:49.8276039 +0800 CST m=+0.061000001
17
1651311049
1651311049828603900
2022-04-30 20:30:49.8286039 +0800 CST m=+10800.062000001
2022-04-30 17:30:49 +0800 CST
2022-04-30
2022-04-30 17:30:49
2022-04-30 17:30
17:30
2019-12-13 00:00:00 +0000 UTC
1576195200
2019-12-13 00:00:00 +0800 CST
1576166400