Go语言时间函数

博客 动态
0 276
优雅殿下
优雅殿下 2022-04-18 22:58:07
悬赏:0 积分 收藏

Go语言 时间函数

@

目录
  • 引言
  • 1. 时间格式化
  • 2. 示例


引言

1946年2月14日,人类历史上公认的第一台现代电子计算机“埃尼阿克”(ENIAC)诞生。

计算机语言时间戳是以1970年1月1日0点为计时起点时间的。计算机诞生为1946年2月14日,而赋予生命力时间是从1970年1月1日0点开始。

1小时=60分钟					Hour1分钟=60秒 					Minute1秒=1000毫秒					Second1毫秒=1000微秒 				Millsecond1微秒=1000纳秒 				Microsecond1纳秒						Nanoseco

1. 时间格式化

2006/1/02 15:04:05
这个时间必须固定不能更改,否则不能获取正确时间

package mainimport (	"fmt"	"time")func main() {	//格式化字符串	now := time.Now()	//时间必须固定不能更改,否则不能获取正确时间	fmt.Println(now.Format("02/1/2006 15:04:05"))	fmt.Println(now.Format("2006/1/02 15:04"))	fmt.Println(now.Format("2006/1/02"))}//输出结果如下07/4/2022 21:51:522022/4/07 21:512022/4/07

2. 示例

  • 对获取的日期进行提取日期并判断是否为会员日
package mainimport (	"fmt"	"strconv"	"strings"	"time")func main() {	//格式化字符串	now := time.Now()	//时间必须固定	times := now.Format("2006/1/02 15:04:05")	a := strings.Fields(times)	fmt.Println(a[0])	fmt.Println(a[1])	b := strings.Split(a[0], "/")	fmt.Println(b[2])		//判断是否为会员日,奇数为会员日,偶数为非会员日	c, _ := strconv.Atoi(b[2])	if c%2 != 0 {		fmt.Println("会员日")	} else {		fmt.Println("非会员日")	}}//输出结果如下2022/4/0722:07:4207会员日
  • 统计程序执行时间,精确到微秒
package mainimport (	"fmt"	"time")func main() {	//使用时间戳统计时间	start := time.Now().UnixNano()	sleepTime()	end := time.Now().UnixNano()	fmt.Printf("程序执行时间: %d", (end-start)/1000)}func sleepTime() {	time.Sleep(time.Millisecond * 100)}//执行结果如下程序执行时间: 113650
posted @ 2022-04-18 22:55 隐姓埋名4869 阅读(0) 评论(0) 编辑 收藏 举报
回帖
    优雅殿下

    优雅殿下 (王者 段位)

    2018 积分 (2)粉丝 (47)源码

    小小码农,大大世界

     

    温馨提示

    亦奇源码

    最新会员