0126-Go-时间

环境

  • Time 2022-08-25
  • Go 1.19

前言

说明

参考:https://gobyexample.com/time

目标

使用 Go 语言的时间。

示例

package main

import (
    "fmt"
    "time"
)

func main() {
    p := fmt.Println

    now := time.Now()
    p(now)

    then := time.Date(
        2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
    p(then)

    p(then.Year())
    p(then.Month())
    p(then.Day())
    p(then.Hour())
    p(then.Minute())
    p(then.Second())
    p(then.Nanosecond())
    p(then.Location())

    p(then.Weekday())

    p(then.Before(now))
    p(then.After(now))
    p(then.Equal(now))

    diff := now.Sub(then)
    p(diff)

    p(diff.Hours())
    p(diff.Minutes())
    p(diff.Seconds())
    p(diff.Nanoseconds())

    p(then.Add(diff))
    p(then.Add(-diff))
}

总结

使用 Go 语言的时间。

附录

posted @ 2022-11-28 22:20  jiangbo4444  阅读(32)  评论(0)    收藏  举报