码迷,mamicode.com
首页 > 其他好文 > 详细

golang -- 时间日期总结

时间:2015-08-31 15:32:40      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

golang时间处理
相关包 "time"

  • 时间戳
    当前时间戳

    ?

    1
    2
    fmt.Println( time .Now().Unix())
    # 1389058332
  • str格式化时间
    当前格式化时间

    ?

    1
    2
    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格式化时间

    ?

    1
    2
    3
    str_time := time .Unix(1389058332, 0).Format( "2006-01-02 15:04:05" )
    fmt.Println(str_time)
    # 2014-01-07 09:32:12
  • str格式化时间转时间戳
    这个比较麻烦

    ?

    1
    2
    3
    4
    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

    ?

    1
    2
    3
    4
    5
    6
    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
  • ing

golang -- 时间日期总结

标签:

原文地址:http://my.oschina.net/ifraincoat/blog/499490

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!