go time时间格式化
时间戳与时间格式相互转化 例一
// 时间戳转时间
str_time := time.Unix(1617279282,0).Format("2006-01-02 15:04:05")
// 时间转时间戳
local, err := time.LoadLocation("Asia/Shanghai") //设置时区
if err != nil {
return 0, err
}
tt, err := time.ParseInLocation("2006-01-02 15:04:05", "2021-04-01 20:14:41", local)
if err != nil {
return 0, err
}
timeUnix := tt.Unix()
// 设置时区,获取当前时间
l,_ := time.LoadLocation("Asia/Shanghai")
fmt.Println(time.Now().In(l).Unix())
时间戳与时间格式相互转化 例二
//将指定时间转为时间戳格式
beforetime := "2020-04-08 00:00:00" //待转化为时间戳的字符串
timeLayout := "2006-01-02 15:04:05" //转化所需模板
loc := time.Now().Location() //获取时区
theTime, _ := time.ParseInLocation(timeLayout, beforetime, loc) //使用模板在对应时区转化为time.time类型
aftertime := theTime.Unix() //转化为时间戳 类型是int64
fmt.Println(theTime) //打印输出theTime 2020-04-08 00:00:00 +0800 CST
fmt.Println(aftertime) //打印输出时间戳 1586275200
//再将时间戳转换为日期
dataTimeStr := time.Unix(aftertime, 0).Format(timeLayout) //设置时间戳 使用模板格式化为日期字符串
fmt.Println(dataTimeStr)
格式化时间
t:= time.Now()
s := t.Format("2006-1-2 15:04:05")
fmt.Println(s) //打印出的格式就是当前的时间 2020-3-31 23:08:35
s := t.Format("2006/1/2")
fmt.Println(s) //打印出的格式就是当前的年月日 2020/3/31
以上的时间格式都是time.Time类型的数据,如果将string类型的字符串时间转为具体时间格式则用time包下的parse函数。
//字符串类型的时间
str := "2020年3月31日"
//第一个参数是模板,第二个是要转换的时间字符串
s, _ := time.Parse("2006年1月2日", str)
fmt.Println(s) //打印出的格式就是2020-03-31 00:00:00 +0000 UTC
//获取年月日信息
year, month, day := time.Now().Date()
fmt.Println(year, month, day) //2020 March 31
//获取时分秒信息
hour, minute, second := time.Now().Clock()
fmt.Println(hour, minute, second) //23 23 54
//获取今年过了多少天了
tday := time.Now().YearDay()
fmt.Println(tday) //91 (今年已经过了91天了)
//获取今天是星期几
weekday := time.Now().Weekday()
fmt.Println(weekday) //Tuesday
获取时间戳
//获取指定日期的时间戳
t := time.Date(2020, 3, 31, 23, 30, 0, 0, time.UTC)
timestamp := t.Unix()
fmt.Println(timestamp) //1585697400
//获取当前时间的时间戳
timestamp2 := time.Now().Unix()
fmt.Println(timestamp2) //1585669151
//当前时间的以纳秒为单位的时间戳
timestamp3 := time.Now().UnixNano()
fmt.Println(timestamp3) //1585669151296330900
时间计算、获取指定前后时间
//时间间隔 相加
now := time.Now()
//当前时间加上一分钟
t := now.Add(time.Minute)
fmt.Println(now) //2020-03-31 23:43:35.0004791 +0800 CST m=+0.002999201
fmt.Println(t) //2020-03-31 23:44:35.0004791 +0800 CST m=+60.002999201
//计算两个时间的间隔
d := t.Sub(now)
fmt.Println(d) //1m0s 相差一分钟
//当前时间的前一天时间
d1 := time.Now().Add(-24 * time.Hour).Format("2006-01-02 15:04:05")
//当前时间的前一年的后两个的前三天
d2 := time.Now().AddDate(-1, 2, -3).Format("2006-01-02 15:04:05")
fmt.Printf("d1 = %v ; d2 = %v\n", d1, d2) //d1 = 2021-11-24 18:27:47 ; d2 = 2021-01-22 18:27:47
var tt int64 = 1617120000
ts := time.Unix(tt, 0)
// AddDate(年,月,日)
tst := ts.AddDate(1, 1, 1)
// 1651420800
fmt.Println(tst.Unix())
UTC时间转标准时间
//UTC时间转标准时间
func (this *DataSearch) UTCTransLocal(utcTime string) string {
t, _ := time.Parse("2006-01-02T15:04:05.000+08:00", utcTime)
return t.Local().Format("2006-01-02 15:04:05")
}
t1 := UTCTransLocal("2021-04-29T14:11:08.000+08:00")
fmt.Println(t1) // 2021-04-29 22:11:08
标准时间转UTC时间
//标准时间转UTC时间
func (this *DataSearch) LocalTransUTC(localTime string) string {
t, _ := time.ParseInLocation("2006-01-02 15:04:05", localTime, time.Local)
return t.UTC().Format("2006-01-02T15:04:05.000+08:00")
}
t2 := LocalTransUTC("2021-04-29 22:11:08")
fmt.Println(t2) // 2021-04-29T14:11:08.000+08:00
go time时间格式化的更多相关文章
- strftime 日期时间格式化
strftime() 函数根据区域设置格式化本地时间/日期,函数的功能将时间格式化,或者说格式化一个时间字符串. size_t strftime(char *strDest,size_t maxsiz ...
- javascript 时间格式化
添加扩展 //时间格式化扩展Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1 ...
- js时间格式化
const formatDate = timestamp => { const date = new Date(timestamp); const m = date.getMonth() + 1 ...
- js对特殊字符转义、时间格式化、获取URL参数
/*特殊字符转义*/ function replace_html(str) { var str = str.toString().replace(/&/g, "&" ...
- 特殊字符转义&时间格式化&获取URL参数
/*特殊字符转义*/ function htmlspecialchars (str) { var str = str.toString().replace(/&/g, "&& ...
- 【AspNetCore】【WebApi】扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat)
扩展Webapi中的RouteConstraint中,让DateTime类型,支持时间格式化(DateTimeFormat) 一.背景 大家在使用WebApi时,会用到DateTime为参数,类似于这 ...
- EasyUI Datagrid Datetime(EasyUI DataGrid 时间格式化)
EasyUI DataGrid 时间格式化 方法一: var Common = { //EasyUI用DataGrid用日期格式化 TimeFormatter: function (value, re ...
- js Date 时间格式化的扩展
js Date 时间格式化的扩展: Date.prototype.format = function (fmt) { var o = { , //月 "d+": this.getD ...
- SqlServer时间格式化
最近用的SqlServer比较多,时间格式化老是忘记,现整理如下:(来源于网上,具体来源地址忘记了,归根到底MSDN吧) SELECT CONVERT(varchar(50), GETDATE(), ...
- js时间格式化(yy年MM月dd日 hh:mm)
//时间格式化 Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, / ...
随机推荐
- 百度高效研发实战训练营-Step4
百度高效研发实战训练营-Step4 4.1 代码检测规则:Java案例详解 以Java的案例进行代码检查规则解释,代码检测规则可以分为以下十类: 4.1.1 源文件规范 该类规范,主要为从文件名.文件 ...
- 《深入理解Mybatis原理》MyBatis数据源与连接池详解
MyBatis数据源DataSource分类 MyBatis把数据源DataSource分为三种: UNPOOLED 不使用连接池的数据源 POOLED 使用连接池的数据源 JNDI 使用JNDI实现 ...
- w3cschool-Netty 实战精髓篇1
https://www.w3cschool.cn/essential_netty_in_action/ Netty 异步和数据驱动 2021-04-22 14:57 更新 在学习Netty的异步和数据 ...
- w3cschool-Hadoop 教程
https://www.w3cschool.cn/hadoop/ 铺垫 人产生数据的速度越来越快,机器则更加快,数据的增长速度通常比算法更快,所以需要另外的一种处理数据的方法. 硬盘的容量增加了,但性 ...
- e-prime3安装
e-prime2.0版本太老,现在安装尝试3.0. 下载 链接: https://pan.baidu.com/s/1XJFDqhoArpIwEf0NpKvoIQ 提取码: h5xk 安装 解压安装包后 ...
- Ubuntu20.04配置CuckooSandbox环境
Ubuntu20.04配置CuckooSandbox环境 因为最近要做恶意软件分析,阅读论文发现动态分析的效果普遍比静态分析的效果要好一些,所以需要搭建一个动态分析的环境,查阅资料发现Cuckoo S ...
- 0425-字符输入流FileReader
package A10_IOStream; import java.awt.datatransfer.StringSelection; import java.io.IOException; impo ...
- Tinyfox 发生重大改版
于2015年6月首次公开发布.为配合Tinyfox的实际应用,在Tinyfox发布后相继推出了 Tinyfox.FastWebApi 和Tinyfox.WebSocket 两个关键的应用框架,构成了相 ...
- 搭建基于Grafana+JMeter+InfluxDB的性能监控与分析平台(Linux)
搭建基于Grafana+JMeter+InfluxDB的性能监控与分析平台(Linux版) 在软件开发和运维领域,性能监控与分析是确保应用稳定性和用户体验的关键环节.随着应用规模的不断扩大和复杂度的增 ...
- nginx 编译安装及配置解析
一.编译安装 安装插件 安装需要的组件 yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel ...