golang的time包:时间字符串和时间戳的相互转换
生活随笔
收集整理的這篇文章主要介紹了
golang的time包:时间字符串和时间戳的相互转换
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本博客轉(zhuǎn)自: https://blog.csdn.net/mirage003/article/details/86073046
package mainimport ("log""time" )func main() {t := int64(1546926630) //外部傳入的時間戳(秒為單位),必須為int64類型t1 := "2019-01-08 13:50:30" //外部傳入的時間字符串//時間轉(zhuǎn)換的模板,golang里面只能是 "2006-01-02 15:04:05" (go的誕生時間)timeTemplate1 := "2006-01-02 15:04:05" //常規(guī)類型timeTemplate2 := "2006/01/02 15:04:05" //其他類型timeTemplate3 := "2006-01-02" //其他類型timeTemplate4 := "15:04:05" //其他類型// ======= 將時間戳格式化為日期字符串 =======log.Println(time.Unix(t, 0).Format(timeTemplate1)) //輸出:2019-01-08 13:50:30log.Println(time.Unix(t, 0).Format(timeTemplate2)) //輸出:2019/01/08 13:50:30log.Println(time.Unix(t, 0).Format(timeTemplate3)) //輸出:2019-01-08log.Println(time.Unix(t, 0).Format(timeTemplate4)) //輸出:13:50:30// ======= 將時間字符串轉(zhuǎn)換為時間戳 =======stamp, _ := time.ParseInLocation(timeTemplate1, t1, time.Local) //使用parseInLocation將字符串格式化返回本地時區(qū)時間log.Println(stamp.Unix()) //輸出:1546926630 } package mainimport ("log""time" ) func main(){// 獲取每天的零點時間戳, 一個小時的時間戳是3600timeStr := time.Now().Format("2006-01-02")t, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local)timeUnix := t.Unix() } package mainimport ("fmt""strconv""time" )func main() {t := time.Now()fmt.Println(t)fmt.Println(t.UTC().Format(time.UnixDate))fmt.Println(t.Unix())timestamp := strconv.FormatInt(t.UTC().UnixNano(), 10)fmt.Println(timestamp)timestamp = timestamp[:10]fmt.Println(timestamp) }//輸出: //2019-09-02 19:17:58.2508394 +0800 CST m=+0.001994001 //Mon Sep 2 11:17:58 UTC 2019 //1567423078 //1567423078250839400 //1567423078 package mainimport ("fmt""strconv""time" )func main() {const longForm = "Jan 2, 2006 at 3:04pm (MST)"t, _ := time.Parse(longForm, "Jun 21, 2017 at 0:00am (PST)")fmt.Println(t)const shortForm = "2006-Jan-02"t, _ = time.Parse(shortForm, "2017-Jun-21")fmt.Println(t)t, _ = time.Parse("01/02/2006", "06/21/2017")fmt.Println(t)fmt.Println(t.Unix())i, err := strconv.ParseInt("1498003200", 10, 64)if err != nil {panic(err)}tm := time.Unix(i, 0)fmt.Println(tm)var timestamp int64 = 1498003200tm2 := time.Unix(timestamp, 0)fmt.Println(tm2.Format("2006-01-02 03:04:05 PM"))fmt.Println(tm2.Format("02/01/2006 15:04:05 PM")) }//輸出 //2017-06-21 00:00:00 +0000 PST //2017-06-21 00:00:00 +0000 UTC //2017-06-21 00:00:00 +0000 UTC //1498003200 //2017-06-21 08:00:00 +0800 CST //2017-06-21 08:00:00 AM //21/06/2017 08:00:00 AM time常用方法After(u Time) bool 時間類型比較,是否在Time之后Before(u Time) bool 時間類型比較,是否在Time之前Equal(u Time) bool 比較兩個時間是否相等IsZero() bool 判斷時間是否為零值,如果sec和nsec兩個屬性都是0的話,則該時間類型為0Date() (year int, month Month, day int) 返回年月日,三個參數(shù)Year() int 返回年份Month() Month 返回月份.是Month類型Day() int 返回多少號Weekday() Weekday 返回星期幾,是Weekday類型ISOWeek() (year, week int) 返回年份,和該填是在這年的第幾周.Clock() (hour, min, sec int) 返回小時,分鐘,秒Hour() int 返回小時Minute() int 返回分鐘Second() int 返回秒數(shù)Nanosecond() int 返回納秒計算時間差
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)) }轉(zhuǎn)載于:https://www.cnblogs.com/nyist-xsk/p/11448348.html
總結(jié)
以上是生活随笔為你收集整理的golang的time包:时间字符串和时间戳的相互转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷P2038 无线网络发射器选址
- 下一篇: springcloud 微服务 分布式