【一Go到底】第二十四天---时间和日期函数
2023-06-13 09:14:30 时间
一、简介
- 因为日期和时间相关的函数存在于time包中,所以需要导入time包
- time.Time 类型 ,表示时间 —> 详见案例解析
- 获取到当前时间的方法 —>
now := time.Now()
// now类型为 time.Time - 格式化时间日期(详见案例)
- 法一 printf
- 法二 printf(now.FOrmat(“2006-01-02 15:04:05”))
- 时间常量 :在程序中获得指定的时间单位的时间
const {
Nanosecond Duration = 1 // 纳秒
Microseond = 1000 * Nanosecond // 微妙
Milliseond = 1000 * Microseond // 毫秒
Second = 1000 * Milliseond // 秒
Minute = 60 * Second //分
Hour = 60 * Minute //时
}
- 休眠 Sleep()
- 获取当前unix时间戳和unixnano时间戳(用于获取随机数字)
二、案例解析
package main
import (
"fmt"
"time"
)
func main() {
// 日期和时间相关函数和方法的使用
// 1.获取当前时间
now := time.Now()
// now = 2022-10-16 12:33:25.9941447 +0800 CST m=+0.002308001, type = time.Time
fmt.Printf("now = %v, type = %T", now, now)
// 去除now中的年 月 日 时分秒
fmt.Printf("年 = %v\n", now.Year())
fmt.Printf("月 = %v\n", now.Month())
fmt.Printf("月 = %v\n", int(now.Month()))
fmt.Printf("日 = %v\n", now.Day())
fmt.Printf("时 = %v\n", now.Hour())
fmt.Printf("分 = %v\n", now.Minute())
fmt.Printf("秒 = %v\n", now.Second())
// 格式化时间
fmt.Printf("当前年月日 %d-%d-%d %d:%d:%d \n", now.Year(),
now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
// 把字符串给一个变量
dataStr := fmt.Sprintf("当前年月日 %d-%d-%d %d:%d:%d \n", now.Year(),
now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
fmt.Printf("dataStr = %v", dataStr)
// 格式化第二种写法
// 2006/01/02 15:04:05 这个字符串是固定的,必须这样写
// 数字不能乱改,间隔可以改
// 可按需求组合
fmt.Printf(now.Format("2006/01/02 15:04:05"))
// 2022/10/16 14:38:34
fmt.Println()
fmt.Printf(now.Format("2006-01-02"))
// 2022-10-16
fmt.Println()
fmt.Printf(now.Format("15:04:06"))
// 14:38:22
fmt.Println()
// 只取月份
fmt.Printf(now.Format("01"))
fmt.Println()
// 只取年
fmt.Printf(now.Format("2006"))
fmt.Println()
// 时间的常量应用
// // 结合Sleep使用
// // 每个一秒打印一个数字,打印到100就退出
// i := 0
// for {
// i++
// fmt.Println("i = ", i)
// //休眠
// time.Sleep(time.Second)
// if 1 == 100 {
// break
// }
// }
// 每隔0.1秒打印一个数字,到100时退出
i := 0
for {
i++
fmt.Println("new i = ", i)
// 0.1秒 = Millisecond * 100
time.Sleep(time.Millisecond * 100)
if i == 100 {
break
}
}
// Unix 和 UnixNano
fmt.Printf("Unix 时间戳:=%v , UnixNano时间戳= %v\n", now.Unix(), now.UnixNano())
// Unix 时间戳:=1665904263 , UnixNano时间戳= 1665904263830448500
}
三、最佳实践(统计函数执行时间)
package main
import (
"fmt"
"strconv"
"time"
)
func test() {
str := ""
for i := 0; i < 100000; i++ {
str += "hello" + strconv.Itoa(i)
}
}
func main() {
start := time.Now().Unix()
test()
end := time.Now().Unix()
fmt.Printf("test函数执行时间为: %v", end-start)
}
相关文章
- Go 爬虫之 HTTP 请求 QuickStart
- Go Performance
- Go 编程 | 连载 32 - 时间与日期
- GO系列(3)-协程 线程 进程 基础
- 如何优雅的通过Shell脚本一键部署GO项目到服务器 |Go主题月
- 微服务配置中心, 这个方案 Go 里用起来不输 SpringCloud
- k8sailor - 用 go 写一个 k8s 后台(第一季完结)
- 开源Web版Linux,数据库,Redis,MongoDB统一管理操作平台Go语言版本
- Go语言纯文本文件的读写操作
- 使用 Go 进行 iOS 和 Android 编程详解手机开发
- Go语言圣经-Deferred函数详解编程语言
- Go_ go mod 命令解决墙的问题详解编程语言
- Go—go-cache包学习详解编程语言
- go语言之goto语句和函数和defer语句详解编程语言
- Linux上构建Go开发环境(linux安装go环境)
- 深度探索Go语言与Redis结合(go-redis)
- 轻松搞定Go语言连接MySQL(go连接mysql)
- 徒手用Go编写Redis迈向新技术世界的旅程(徒手用go写个redis)
- 用Go语言深入Redis缓存技术(用go写redis)
- Go语言链接Oracle数据库的实践记录(go 链接oracle)
- 一步一步学习Go读取Oracle数据库(go读取oracle)
- Go语言搭建Oracle数据库快速连接方法(go语言连接oracle)
- Go语言操作Oracle数据库技术实践(go操作oracle)
- 踏上oracle编程之路,Go(go oracle编程)
- 程序Go语言调用Oracle数据库驱动程序指南(go oracle驱动)
- Redis集群搭建Go的艰难之路(redis集群搭建go)