-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
golang time使用 #48
Comments
func testTime() {
//获取时间戳
timestamp := time.Now().Unix()
fmt.Println(timestamp)
//格式化为字符串,tm为Time类型
tm := time.Unix(timestamp, 0)
fmt.Println(tm.Format("2006-01-02 03:04:05 PM"))
fmt.Println(tm.Format("02/01/2006 15:04:05 PM"))
const longForm = "2006-01-02 15:04:05"
tt, _ := time.Parse(longForm, "2018-03-12 23:11:00")
tt2, _ := time.Parse(longForm, "2018-03-12 23:08:00")
ttValue := tt.Unix()
tt2Value := tt2.Unix()
fmt.Println("ttValue - tt2Value =", ttValue-tt2Value)
t := time.Unix(1362984425, 0)
nt := t.Format("2006-01-02 15:04:05")
fmt.Println(nt)
const TimeFormat = "2006-01-02 15:04:05"
t2, _ := time.Parse(TimeFormat, "2013-08-11 11:18:46")
fmt.Println(t2)
// c := time.Tick(1 * time.Second)
// for now := range c {
// fmt.Printf("%v %s\n", now, "ss")
// }
unixSeconds := time.Now().Unix()
fmt.Println("unixSeconds =>", unixSeconds)
tm2, _ := time.Parse("2006-01-02 00:00:00", "2018-03-12 14:00:00")
fmt.Println("tm2.Unix =>", tm2.Unix())
year, month, day := time.Now().Date()
fmt.Println("Happy Go day!", year, month, day)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
done := make(chan bool)
go func() {
time.Sleep(10 * time.Second)
done <- true
}()
for {
select {
case <-done:
fmt.Println("Done!")
return
case t := <-ticker.C:
fmt.Println("Current time: ", t)
}
}
} |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
当前时间戳
fmt.Println(time.Now().Unix())
当前格式化时间
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
时间戳转str格式化时间
str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")
str格式化时间转时间戳
The text was updated successfully, but these errors were encountered: