日期時間
尚硅谷Golang課
日期時間
func main() {
//1.獲取當前本地時間
now := time.Now()
fmt.Printf("now=%v,type=%T", now, now) //返回的類型是struct
//2.通過now獲取各種詳細
fmt.Printf("年=%v,type=%T\n", now.Year(), now.Year())
fmt.Printf("月=%v,type=%T\n", now.Month(), now.Month())
fmt.Printf("月=%v,type=%T\n", int(now.Month()), int(now.Month()))
fmt.Printf("日=%v,type=%T\n", now.Day(), now.Day())
fmt.Printf("時=%v,type=%T\n", now.Hour(), now.Hour())
fmt.Printf("分=%v,type=%T\n", now.Minute(), now.Minute())
fmt.Printf("秒=%v,type=%T\n", now.Second(), now.Second())
fmt.Printf("星期=%v,type=%T\n", now.Weekday(), now.Weekday())
fmt.Printf("星期=%v\n", int(now.Weekday()))
//3.格式化日期時間
dateStr := fmt.Sprintf("年月日時分=%d-%d-%d %d:%d\n", now.Year(), now.Month(),
now.Day(), now.Minute(), now.Second())
fmt.Println(dateStr)
//4.蛋疼的指定格式
fmt.Println(now.Format("2006-01-02 15:04:05"))
fmt.Println(now.Format("2006-01-02"))
fmt.Println(now.Format("06"))
fmt.Println(now.Format("01"))
fmt.Println(now.Format("03"))
fmt.Println(now.Format("01-02-06 03 04 05"))
//123456=月日時分秒年
//5.時間常量&休眠
//每隔固定時間打印數字,打到100退出
for i := 0; i <= 100; i++ {
fmt.Println(i)
time.Sleep(time.Millisecond)
}
//6.獲取時間戳unix unixNano
fmt.Println(now.Unix())
fmt.Println(now.UnixNano())
//跑之前的時間
start := now.Unix()
test()
end := now.Unix()
fmt.Printf("耗時%v", end-start)
}
//7.統計函數執行時間
func test() {
str := ""
for i := 0; i < 100000; i++ {
str = str + "hello" + strconv.Itoa(i)
}
}
上次修改於 2021-08-01
此篇文章的評論功能已經停用。