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, / ...
随机推荐
- Qt开发经验小技巧181-185
Qt天生就是linux的,从linux开始发展起来的,所以不少Qt程序员经常的开发环境是linux,比如常用的ubuntu等系统,整理了一点常用的linux命令. 命令 功能 sudo -s 切换到管 ...
- Qt编写项目作品26-一维码二维码解析及生成
一.功能特点 支持本地USB摄像头实时解析. 支持网络视频流实时解析. 解码格式支持一维码二维码等各种编码. 可生成一维码二维码,一维码支持EAN_13格式,其他格式可定制. 条形码参数支持宽度.高度 ...
- springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController required a bean of type 'com.cetc.UserManger.service.UserService' that could not be found.
错误描述: springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController r ...
- DVWA靶场Brute Force (暴力破解) 漏洞low(低),medium(中等),high(高),impossible(不可能的)所有级别通关教程及代码审计
暴力破解 暴力破解是一种尝试通过穷尽所有可能的选项来获取密码.密钥或其他安全凭证的攻击方法.它是一种简单但通常无效率的破解技术,适用于密码强度较弱的环境或当攻击者没有其他信息可供利用时.暴力破解的基本 ...
- 一套十万级TPS的IM综合消息系统的架构实践与思考
本文由作者jhon_11分享,有大量修订和改动. 1.引言 如何设计一款高性能.高并发.高可用的im综合消息平台是很多公司发展过程中会碰到且必须要解决的问题.比如一家公司内部的通讯系统.各个互联网平台 ...
- Solution Set -「NOIP Simu.」20221008
\(\mathscr{A}\sim\)「CF 1680E」Moving Chips Link & Submission. Tag:「水题无 tag」 温暖签到惹, DP 一下就好了 ...
- w3cschool-Storm 入门教程
Storm 基础知识 基础知识 Storm 是一个分布式的,可靠的,容错的数据流处理系统.它会把工作任务委托给不同类型的组件,每个组件负责处理一项简单特定的任务.Storm 集群的输入流由一个被称作 ...
- C#添加log4日志
第一步导入log4net 在vs的程序包管理器控制台中执行命令 NuGet\Install-Package log4net -Version 2.0.0 第二步加帮助类HttpHelper using ...
- 从Linux零拷贝深入了解I/O
转载&学习文章:从Linux零拷贝深入了解I/O 本文将从文件传输场景以及零拷贝技术深究 Linux I/O 的发展过程.优化手段以及实际应用. 前言 存储器是计算机的核心部件之一,在完全理想 ...
- linux:安装php7.x
参考:链接 更新yum源 CentOS/RHEL 7.x: rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.n ...