神剑山庄资源网 Design By www.hcban.com
1. 获取当前时间字符串和时间戳
package main import ( "fmt" "time" ) func main() { now := time.Now().UTC() // 显示时间格式: UnixDate = "Mon Jan _2 15:04:05 MST 2006" fmt.Printf("%s\n", now.Format(time.UnixDate)) // 显示时间戳 fmt.Printf("%ld\n", now.Unix()) // 显示时分:Kitchen = "3:04PM" fmt.Printf("%s\n", now.Format("3:04PM")) }
更多时间格式
const ( ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" )
2. 时间字符串解析成时间格式
package main import ( "fmt" "time" ) func main() { timeStr := "2018-01-01" fmt.Println("timeStr:", timeStr) t, _ := time.Parse("2006-01-02", timeStr) fmt.Println(t.Format(time.UnixDate)) }
3. 获取当天零点时间戳
方法1
package main import ( "fmt" "time" ) func main() { timeStr := time.Now().Format("2006-01-02") t, _ := time.Parse("2006-01-02", timeStr) fmt.Println(t.Format(time.UnixDate)) //Unix返回早八点的时间戳,减去8个小时 timestamp := t.UTC().Unix() - 8*3600 fmt.Println("timestamp:", timestamp) }
方法2
package main import ( "fmt" "time" ) func main() { now := time.Now() t, _ := time.ParseInLocation("2006-01-02", now.Format("2006-01-02"), time.Local) timestamp := t.Unix() fmt.Println(timestamp) } /* time.Local本地时区 var Local *Location = &localLoc 以及UTC时区 var UTC *Location = &utcLoc 还可以替换成指定时区 //func LoadLocation(name string) (*Location, error) loc, _ := time.LoadLocation("Europe/Berlin") If the name is "" or "UTC", LoadLocation returns UTC. If the name is "Local", LoadLocation returns Local. */
补充:Golang中获取当天0点的格式化时间
如下所示:
EndDateLimit := time.Now().Format("2006-01-02 00:00:00")
取当天某个整点的时间戳
now := time.Now().Unix() muteEndTime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 8, 0, 0, 0, time.Local).Unix() muteStartTime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 22, 0, 0, 0, time.Local).Unix()
本地当前时间戳(10位)
fmt.Println(time.Now().Unix()) //1468479251
本地当前时间戳(19位)
fmt.Println(time.Now().UnixNano()) //1468480006774460462
时间戳转时间
fmt.Println(time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")) //2014-01-07 09:32:12
时间转时间戳
fmt.Println(time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local).Unix())
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无golang时间字符串和时间戳转换的案例的评论...
更新日志
2024年10月04日
2024年10月04日
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]
- 班得瑞原装进口《第九张新世纪专辑:微风山谷》1CD[APE/CUE分轨][331MB]
- 陈慧琳.2008-Love.Fighters演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 米志宏.1994-HAPPY1994【波丽佳音】【WAV+CUE】
- 潘美辰.2006-最冷的夏天(引进版)【美梦辰真】【WAV+CUE】
- 班得瑞原装进口《第十张新世纪专辑:月光水岸》1CD[APE/CUE分轨][277.2MB]
- 班得瑞原装进口《第十一张新世纪专辑:雾色山脉》1CD[APE/CUE分轨][291.6MB]