Skip to content
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

Open
hapiman opened this issue Mar 12, 2018 · 2 comments
Open

golang time使用 #48

hapiman opened this issue Mar 12, 2018 · 2 comments

Comments

@hapiman
Copy link
Owner

hapiman commented Mar 12, 2018

  • 当前时间戳
    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_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local)
unix_time := the_time.Unix()
fmt.Println(unix_time)
  • 使用time.Parse
the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")
if err == nil {
        unix_time := the_time.Unix()
	fmt.Println(unix_time)		
}
@hapiman
Copy link
Owner Author

hapiman commented Mar 12, 2018

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)
		}
	}
}

@hapiman
Copy link
Owner Author

hapiman commented Mar 24, 2018

  • ddd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant