• Go语言time包的使用


    时间类型

    time.Time类型表示时间。

    1、时间类型

    func timeDemo()  {
        now := time.Now()
        fmt.Println(now) 
        fmt.Println(now.Format("2006-01-02 15:04:05"))
        year := now.Year()
        month := now.Month()
        day := now.Day()
        hour := now.Hour()
        minute := now.Minute()
        second := now.Second()
        //输出格式可以自定义
        fmt.Printf("%d,%02d,%02d %02d:%02d:%02d
    ", year, month, day, hour, minute, second) 
    } 

    输出结果:

    2021-07-29 15:48:56.927489 +0800 CST m=+0.000069188
    2021-07-29 15:48:56
    2021,07,29 15:48:56
    

    2、字符串类型转time

        s4 := "1999年10月19日" //字符串
        t4, err := time.Parse("2006年01月02日", s4)
        if err != nil {
            fmt.Println("err", err)
        }
        fmt.Println(t4)
    

    输出结果:   

    1999-10-19 00:00:00 +0000 UTC 

    3、时间戳

    时间戳是自1970年1月1日(08:00:00GMT)至当前时间的总毫秒数。它也被称为Unix时间戳(UnixTimestamp)。

    //时间戳
    func timestampDemo()  {
        now := time.Now()
        timestamp := now.Unix()
        fmt.Println(timestamp)  
    }

    输出结果:

    1627545363

    time.Unix()函数将时间戳转为时间格式。

    func timestampDemo2(timestamp int64) {
    	now := time.Now()
    	timestamp := now.Unix()
    	timeObj := time.Unix(timestamp, 0) //将时间戳转为时间格式
    	fmt.Println(timeObj)    //2021-07-29 15:57:15 +0800 CST
    	year := timeObj.Year()     //年
    	month := timeObj.Month()   //月
    	day := timeObj.Day()       //日
    	hour := timeObj.Hour()     //小时
    	minute := timeObj.Minute() //分钟
    	second := timeObj.Second() //秒
    	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d
    ", year, month, day, hour, minute, second) //2021-07-29 15:57:15
    }
    

    输出结果:

    2021-07-29 15:57:15 +0800 CST
    2021-07-29 15:57:15

    4、时间间隔

    Duration类型代表两个时间点之间经过的时间,以纳秒为单位。

    const (
        Nanosecond  Duration = 1
        Microsecond          = 1000 * Nanosecond
        Millisecond          = 1000 * Microsecond
        Second               = 1000 * Millisecond
        Minute               = 60 * Second
        Hour                 = 60 * Minute
    )

    5、时间加时间间隔

    语法:

    func (t Time) Add(d Duration) Time

    示例:计算1小时后及1小时前时间

    func main() {
        now := time.Now()
        later := now.Add(time.Hour)
        fmt.Println(later)
        before := now.Add(-time.Hour)
        fmt.Println(before)
    }
    

    输出结果:

    2021-07-29 17:01:50.239301 +0800 CST m=+3600.000080244
    2021-07-29 15:01:50.239301 +0800 CST m=-3599.999919756
    

    6、定时器

    time.Tick(时间间隔)来设置定时器。

    //定时器,每隔1s打印下i
    func tickDemo() {
        ticker := time.Tick(time.Second)
        for i := range ticker{
            fmt.Println(i)
        }
    }
    

    输出结果:

    2021-07-29 16:03:16.519927 +0800 CST m=+1.003226534
    2021-07-29 16:03:17.516987 +0800 CST m=+2.000315947
    2021-07-29 16:03:18.516788 +0800 CST m=+3.000145536
    ....

    7、睡眠sleep

    time.Sleep(3 *time.Second)
    fmt.Println("睡眠3s")

    睡眠[1-10随机数]

    func main() {
    	rand.Seed(time.Now().UnixNano())
    	randNum := rand.Intn(10) + 1 //int类型,需要转换
    	fmt.Println(randNum)
    	time.Sleep(time.Duration(randNum) * time.Second)
    	fmt.Printf("睡了%d秒
    ", randNum)
    }
    

    输出结果:

    6
    睡了6秒

    8、时间格式化

    Go语言中格式化时间模板不是常见的Y-m-d H:M:S而是使用Go的诞生时间2006年1月2号15点04分(记忆口诀为2006 1 2 3 4 5)。

    //时间格式化
    func formatDemo() {
        now := time.Now()
        // 格式化的模板为Go的出生时间2006年1月2号15点04分
        fmt.Println(now.Format("2006-01-02 15:04:05"))  //2019-04-24 18:37:59
        fmt.Println(now.Format("2006/01/02 15:04"))
        fmt.Println(now.Format("15:04 2006/01/02"))
        fmt.Println(now.Format("2006/01/02"))
    }
    

    输出结果:

    2021-07-29 16:12:52
    2021/07/29 16:12
    16:12 2021/07/29
    2021/07/29  

    9、时间比较

    Equal

    判断时间是否相等,会有时区影响。

    func (t Time) Equal(u Time) bool

    Before

    func (t Time) Before(u Time) bool

    如果t代表的时间点在u之前,返回真;否则返回假。

    After

    func (t Time) After(u Time) bool

    如果t代表的时间点在u之后,返回真;否则返回假。

  • 相关阅读:
    画了朵花
    定位
    浮动
    盒模型
    html+css笔记1
    闭包
    高阶函数
    函数
    Map Set iterable
    git stash clear/drop 后代码如何恢复
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/15075612.html
Copyright © 2020-2023  润新知