Go學習筆記:time包使用

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

摘要: 和python一樣,golang時間處理還是比較方便的,以下按照之前python的時間處理展開

golang時間處理
相關包 "time"

  • 時間戳記 
    目前時間戳

    fmt.Println(time.Now().Unix())# 1389058332
  • str格式化時間
    當前格式化時間

    fmt.Println(time.Now().Format("2006-01-02 15:04:05"))  // 這是個奇葩,必須是這個時間點, 據說是go誕生之日, 記憶方法:6-1-2-3-4-5# 2014-01-07 09:42:20
  • 時間戳記轉str格式化時間

    str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")fmt.Println(str_time)# 2014-01-07 09:32:12
  • str格式化時間轉時間戳記
    這個比較麻煩

    the_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local)unix_time := the_time.Unix()fmt.Println(unix_time)# 389045004

    還有一種方法,使用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)}# 1389171881
轉自:https://my.oschina.net/1123581321/blog/190942


package mainimport (    "fmt"    "time")func main() {    t, _ := time.Parse("2006-01-02 15:04:05", "2016-04-20 16:23:00")    fmt.Println(t.Unix())    y, m, d := time.Unix(1466344320, 0).Date()    fmt.Println(y, m, d)    //format後面的字串必須是2006-01-02 15:04:05,據說go是這個時間誕生的    fmt.Println(time.Now().Format("2006-01-02 15:04:05"))    fmt.Println(time.Now().Format("2006-01-02"))    fmt.Println(time.Now().Format("20060102"))                                                                                                                                select {    case <-time.After(5 * time.Second):        fmt.Println("After 5 second")    }       c := time.Tick(10 * time.Second)    for now := range c {         fmt.Println(now)    }   }







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.