golang时间和日期相关函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | //时间和日期相关函数 func main() { //1.获取当前时间 now := time.Now() fmt.Printf( "now = %v, type = %T" , now, now) //2.通过now获取年月日时分秒 fmt.Println( "年= " , now.Year()) fmt.Println( "月= " , now.Month()) fmt.Println( "日= " , now.Day()) fmt.Println( "时= " , now.Hour()) fmt.Println( "分= " , now.Minute()) fmt.Println( "秒= " , now.Second()) //格式化日期时间 fmt.Printf( "当前年月日 %d-%d-%d %d:%d:%d\n" , now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) dateStr := fmt.Sprintf( "当前年月日 %d-%d-%d %d:%d:%d\n" , now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) fmt.Printf( "dateStr=%v\n" , dateStr) //格式化日期的第二种方式time.Format() fmt.Println(now.Format( "2006-01-02 15:04:05" )) fmt.Println(now.Format( "2006-01-02" )) fmt.Println(now.Format( "15:04:05" )) //需求,每隔一秒钟打印一个数字,打印到100退出 /* 时间常量: Nanosecond = 1纳秒 Microsecond = 1000*Nanosecond微秒 Millisecond = 1000*Microsecond毫秒 */ i := 0 for { i++ fmt.Println(i) //休眠 time.Sleep(time.Second) if i == 100 { break } } //时间戳(用于获取随机数字) //Unix和UnixNano的使用 fmt.Printf( "Unix时间戳=%v Unixnano时间戳=%v" , now.Unix(), now.UnixNano()) } |
计算拼接十万个字符所用时长
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | func main() { start := time.Now().Unix() timetest() end := time.Now().Unix() fmt.Println( "所用时间为 " , end-start) } func timetest() { str := "hello" for i := 0; i < 100000; i++ { str = str + strconv.Itoa(i) } } |
__EOF__

本文作者:StarGazer
本文链接:https://www.cnblogs.com/PatrickStarGazer/p/15927195.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/PatrickStarGazer/p/15927195.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具