您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)使用golang語言怎么將時(shí)間字符串與時(shí)間戳進(jìn)行轉(zhuǎn)換,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
package main import ( "fmt" "time" ) func main() { now := time.Now().UTC() // 顯示時(shí)間格式: UnixDate = "Mon Jan _2 15:04:05 MST 2006" fmt.Printf("%s\n", now.Format(time.UnixDate)) // 顯示時(shí)間戳 fmt.Printf("%ld\n", now.Unix()) // 顯示時(shí)分:Kitchen = "3:04PM" fmt.Printf("%s\n", now.Format("3:04PM")) }
更多時(shí)間格式
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" )
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)) }
方法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返回早八點(diǎn)的時(shí)間戳,減去8個(gè)小時(shí) 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本地時(shí)區(qū) var Local *Location = &localLoc 以及UTC時(shí)區(qū) var UTC *Location = &utcLoc 還可以替換成指定時(shí)區(qū) //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. */
補(bǔ)充:Golang中獲取當(dāng)天0點(diǎn)的格式化時(shí)間
如下所示:
EndDateLimit := time.Now().Format("2006-01-02 00:00:00")
取當(dāng)天某個(gè)整點(diǎn)的時(shí)間戳
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()
本地當(dāng)前時(shí)間戳(10位)
fmt.Println(time.Now().Unix()) //1468479251
本地當(dāng)前時(shí)間戳(19位)
fmt.Println(time.Now().UnixNano()) //1468480006774460462
時(shí)間戳轉(zhuǎn)時(shí)間
fmt.Println(time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")) //2014-01-07 09:32:12
時(shí)間轉(zhuǎn)時(shí)間戳
fmt.Println(time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local).Unix())
關(guān)于使用golang語言怎么將時(shí)間字符串與時(shí)間戳進(jìn)行轉(zhuǎn)換就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。