您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关使用golang语言怎么将时间字符串与时间戳进行转换,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
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" )
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返回早八点的时间戳,减去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())
关于使用golang语言怎么将时间字符串与时间戳进行转换就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。