@


引言

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

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

1小时=60分钟					Hour
1分钟=60秒 Minute
1秒=1000毫秒 Second
1毫秒=1000微秒 Millsecond
1微秒=1000纳秒 Microsecond
1纳秒 Nanoseco

1. 时间格式化

2006/1/02 15:04:05

这个时间必须固定不能更改,否则不能获取正确时间

package main

import (
"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:52
2022/4/07 21:51
2022/4/07

2. 示例

  • 对获取的日期进行提取日期并判断是否为会员日
package main

import (
"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/07
22:07:42
07
会员日
  • 统计程序执行时间,精确到微秒
package main

import (
"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

最新文章

  1. nodejs框架express快速开始
  2. Node.js 快速了解
  3. EL表达式的操作符
  4. PHP —— 读取文件到二维数组
  5. Machine Learning : Pre-processing features
  6. [React Native] Create a component using ScrollView
  7. list.h
  8. php的系统常量
  9. [原创]抢先DriverStudio夺取机器控制权(下篇)
  10. PADS无模命令总结
  11. Phpcms 详情页显示所属栏目的同级栏目
  12. Android 开发使用第三方库出现Crash时处理方案汇总
  13. 深入浅出——float
  14. 服务端、实时、大数据、AI计算
  15. IO-MYSQL的理解
  16. RabbitMQ学习笔记(二):基础概念
  17. redis资料
  18. LeetCode-Microsoft-Clone Graph
  19. Android Bug BaseExpandableListAdapter, getChildView
  20. JAVA容器-模拟ArrayList的底层实现

热门文章

  1. Mysql学习笔记-InnoDB深度解析
  2. 老徐和阿珍的故事:ArrayList和LinkedList的效率到底哪个高?
  3. 用腾讯云Gogs搭建私有git服务器
  4. dp:最长非递减序列
  5. Spring 框架中都用到了哪些设计模式?
  6. 学习Jenkins(二)
  7. 时间工具类之"获取相差天数"
  8. 10.Flink实时项目之订单维度表关联
  9. 1_开环系统和闭环系统_反馈控制_Open/Closed Loop System_Feedback
  10. 安装Prettier