go time包

go time包

/**
* @Author: wsp
* @Date: 2018/1/18 10:17
* @Description:
 */
package timeStudy

import (
    "fmt"
    "time"
)

func TimeStudy1() {

    // 时间转string
    timeNow := time.Now()
    // 获取当前时间
    fmt.Println(timeNow)
    fmt.Println("=====================")

    // 格式化时间,格式必须为:2006-01-02 15:04:05
    format := timeNow.Format("2006-01-02 15:04:05")
    fmt.Println(format)

    // string 转时间 layout:格式必须为:2006-01-02 15:04:05
    t, _ := time.Parse("2006-01-02 15:04:05", "2014-06-15 08:37:18")
    fmt.Println("========================")
    fmt.Println(t)

    fmt.Println("========================")
    // 判断时间的前后
    fmt.Println(t.Before(timeNow))
    fmt.Println(t.After(timeNow))

    // 返回时间戳,长度为10
    fmt.Println("========================")
    fmt.Println(time.Now().Unix())

    // 自定义时间对象
    fmt.Println("========================")
    fmt.Println(time.Date(2017, 02, 27, 20, 20, 20, 20, time.Local))

    // 字符串转时间戳
    fmt.Println("========================")
    fmt.Println(t.Unix())

    //timeNow1 := time.Now(),获取某个时间与当前时间差
    fmt.Println("========================")
    fmt.Println(time.Since(timeNow))

    fmt.Println("========================")
}

运行结果:

2018-01-18 10:51:49.145745 +0800 CST
=====================
2018-01-18 10:51:49
========================
2014-06-15 08:37:18 +0000 UTC
========================
true
false
========================
1516243909
========================
2017-02-27 20:20:20.00000002 +0800 CST
========================
1402821438
========================
35.0245ms
========================
1h0m0s

Process finished with exit code 0

 

posted @ 2018-01-18 10:55  穷开心y  阅读(234)  评论(0编辑  收藏  举报