工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆

golang时间函数处理

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

 

http://studygolang.com/articles/669
--------------------

Go 如何获取三个月前的月份日期

  1. func main () {
  2. now := time.Now()
  3.  
  4. yesterday := now.AddDate( 0, -1, 0 )
  5. bef_yes := yesterday.AddDate ( 0, -1, 0 )
  6.  
  7. fmt.Printf("Today:%s\n", now.Format("200601"))
  8. fmt.Printf("Yesterday:%s\n", yesterday.Format("200601"))
  9. fmt.Printf("Yesterdat before Yesterday:%s\n",bef_yes.Format("200601"))
 http://www.golangtc.com/t/51bd288c320b5264b800001c
 
--------------
时间比较

先把当前时间格式化成相同格式的字符串,然后使用time的Before, After, Equal 方法即可.

1
2
3
4
5
6
7
8
9
time1 := "2015-03-20 08:50:29"
time2 := "2015-03-21 09:04:25"
//先把时间字符串格式化成相同的时间类型
t1, err := time.Parse("2006-01-02 15:04:05", time1)
t2, err := time.Parse("2006-01-02 15:04:05", time2)
if err == nil && t1.Before(t2) {
  //处理逻辑
  fmt.Println("true")
}
 http://www.jb51.net/article/64705.htm
--------------
golang time 时间的加减法
http://blog.csdn.net/codyguo/article/details/53009451
 

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8

发表评论

评论内容 (必填):