golang time 时间 加减法
time包中的Add和Sub的用法,Add用于计算某个时间之前和之后的时间点,Sub用于计算两个时间差
实例如下:
package mainimport ("fmt""strings""time"
)func main() {// Add 时间相加now := time.Now()// ParseDuration parses a duration string.// A duration string is a possibly signed sequence of decimal numbers,// each with optional fraction and a unit suffix,// such as "300ms", "-1.5h" or "2h45m".// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".// 10分钟前m, _ := time.ParseDuration("-1m")m1 := now.Add(m)fmt.Println(m1)// 8个小时前h, _ := time.ParseDuration("-1h")h1 := now.Add(8 * h)fmt.Println(h1)// 一天前d, _ := time.ParseDuration("-24h")d1 := now.Add(d)fmt.Println(d1)printSplit(50)// 10分钟后mm, _ := time.ParseDuration("1m")mm1 := now.Add(mm)fmt.Println(mm1)// 8小时后hh, _ := time.ParseDuration("1h")hh1 := now.Add(hh)fmt.Println(hh1)// 一天后dd, _ := time.ParseDuration("24h")dd1 := now.Add(dd)fmt.Println(dd1)printSplit(50)// Sub 计算两个时间差subM := now.Sub(m1)fmt.Println(subM.Minutes(), "分钟")sumH := now.Sub(h1)fmt.Println(sumH.Hours(), "小时")sumD := now.Sub(d1)fmt.Printf("%v 天\n", sumD.Hours()/24)}func printSplit(count int) {fmt.Println(strings.Repeat("#", count))
}
golang time 时间 加减法相关推荐
- golang 格式化时间成datetime
Golang或者Beego,总需要往数据库里写datetime时间戳. Golang对时间格式支持并不理想. 先看一个例子: package main import ( "fmt" ...
- python计算时间差代码_Python3显示当前时间、计算时间差及时间加减法示例代码...
Python3显示当前时间.计算时间差及时间加减法示例代码 摘要 在使用Python写程序时,经常需要输出系统的当前时间以及计算两个时间之间的差值,或者将当前时间加减一定时间(天数.小时.分钟.秒)来 ...
- golang日期时间格式format()
format()函数格式化字符串,用了语句time.now().format("2015-11-12 12:00:00"),结果输出结果就是不能达到理想的结果,然后把golang文 ...
- golang实现时间轮
golang原生定时器 http://c.biancheng.net/view/5723.html https://www.cyhone.com/articles/analysis-of-golang ...
- golang格式化时间精确到毫秒
如题,我们可能希望格式化时间精确到毫秒,可以这样做: start_time := time.Now() yyyyMMddHHmmss := start_time.Format("2006-0 ...
- Excel中如何时间加减法
今天是用Excel表格想要计算时间,给出的是类似于C2:9:00 D2: 19:00 的格式,不知道该怎么计算 后来发现Excel表格式按照天作为计算单位的,并且在设置单元格格式中,会根据用户输 ...
- java输出减法表,Calendarjava时间加减法和格式化输出
Calendar calendar = Calendar.getInstance(); //减三天 calendar.add(5, -3); //将Calendar类型转换成Date类型 Date t ...
- golang获取时间所在周的起止时间
1.分享一下翻译器的文件 type Transform struct {Value interface{}Time time.Time }var (timeLayoutMap = map[string ...
- calender加3天_JAVA日期或者时间加减法,加上几天或者减去
/** * 日期的加减方法 * 用于在当前的天或者小时或者分钟或者月份的基础上加上或者减去若干小时,分钟,日,月 * @param currentDay 当前月份的某一天 * @param day ( ...
最新文章
- 构建Docker镜像(三)
- 【Spring实战】—— 3 使用facotry-method创建单例Bean总结
- python qtdesigner 提升类_python3+PyQt5+Qt Designer实现扩展对话框
- 微服务网关 Kong 科普
- 腾讯商业数据分析师培养计划
- python 绘制二维曲面_用python绘制曲面[复制]
- docker gogs安装
- Vuex actions 异步操作基础
- 一个web项目web.xml的配置中context-param配置作用
- php局部缓存,Smarty局部缓存的几种方法简介_php实例
- 关机重启注销慢? 头疼? 提高速度的好办法!
- Dell 笔记本触摸板网页双指滑动黑屏
- git拉取代码提示:本地分支 has no tracked branch
- 职业院校教师招聘结构化面试
- 《咸鱼分享》咸鱼在线TOP
- 瑞禧研究Boc-Val-Dil-Dap及其衍生物(OH/Phe-OMe/Doe/Dap-Nrp/)——抗体药物偶联物
- docker部署微服务项目
- Docker Registry部署镜像私有仓库及鉴权认证
- React教程(六)——使用 create-react-app 快速构建 React 开发环境
- 算法题 —— 寻找明星
热门文章
- 2010.4.18 OA 项目组一周工作报告
- node.js安装模式 的区别_Nodejs入门篇(一)
- 5G NGC — AMF 接入和移动性管理功能
- VIM — vim-go
- The connected J-Link is defective,Proper operation cannot be guaranteed......的解决办法
- Altium Designer从已有的PCB图中导出封装库
- STM32F10x命名规则
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
- Python调用MySQL模块初试
- Adobe将于2020年末停止对Flash的支持