//【时间获取及格式化】
//获取当前时间
now_time := time.Now()
fmt.Printf("Now_time=%v,数据类型:%T \n", now_time, now_time)
//通过now获取年月日时分秒
fmt.Printf("年=%v\n", now_time.Year())
fmt.Printf("月=%v\n", now_time.Month()) //默认是英文的月份
fmt.Printf("月=%v\n", int(now_time.Month())) //加int转换为数字
fmt.Printf("日=%v\n", now_time.Day())
fmt.Printf("时=%v\n", now_time.Hour())
fmt.Printf("分=%v\n", now_time.Minute())
fmt.Printf("秒=%v\n", now_time.Second())
//格式化日期时间①
fmt.Printf("当前时间 %d-%d-%d %d:%d:%d \n", now_time.Year(), int(now_time.Month()), now_time.Day(), now_time.Hour(), now_time.Minute(), now_time.Second())
//把格式化好的时间返回给一个变量,然后输出
date_now := fmt.Sprintf("当前时间 %d-%d-%d %d:%d:%d \n", now_time.Year(), int(now_time.Month()), now_time.Day(), now_time.Hour(), now_time.Minute(), now_time.Second())
fmt.Printf("date:%v\n", date_now)
//格式化日期时间②
//2006/01/02 15:04:05 这里必须数字一个不差的写
golang时间函数
最新推荐文章于 2024-04-18 03:00:45 发布
本文深入探讨了Go语言中处理时间的函数,包括获取当前时间、时间格式化、时间比较和时间操作等核心概念,帮助开发者更好地理解和使用Go的time包。
摘要由CSDN通过智能技术生成