Go:官方库 - Time常用方法_了-凡的博客-CSDN博客

文章目录

1、时间初始化

1、 本地时区初始化

    time.Now() //当前时间

    time.Date(2018, 1, 2, 15, 30, 10, 0, time.Local) //初始化时间

2、自定义时区初始化

      // func (t Time) In(loc *Location) Time 当前时间对应指定时区的时间
      loc, err := time.LoadLocation("America/Los_Angeles")
      
      if err == nil{
          now : time.Now().In(loc)
          fmt.println(now)
      }
    • *

2、时间运算

1、在某个时间之前

    // Before reports whether the time instant t is before u.
    func (t Time) Before(u Time) bool 

2、在某个时间之后

    // After reports whether the time instant t is after u.
    func (t Time) After(u Time) bool 

3、时间相等

    // Equal reports whether t and u represent the same time instant.
    // Two times can be equal even if they are in different locations.
    // For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
    // See the documentation on the Time type for the pitfalls of using == with
    // Time values; most code should use Equal instead.
    func (t Time) Equal(u Time) bool 

4、加时间

加上一个Duration的时间戳后的时间。

    // Add returns the time t+d.
    func (t Time) Add(d Duration)

加上多少年多少月多少天。

    // AddDate returns the time corresponding to adding the
    // given number of years, months, and days to t.
    // For example, AddDate(-1, 2, 3) applied to January 1, 2011
    // returns March 4, 2010.
    //
    // AddDate normalizes its result in the same way that Date does,
    // so, for example, adding one month to October 31 yields
    // December 1, the normalized form for November 31.
    func (t Time) AddDate(years int, months int, days int)

5、减时间

减去一个Duration的时间戳后的时间。

    // Sub returns the duration t-u. If the result exceeds the maximum (or minimum)
    // value that can be stored in a Duration, the maximum (or minimum) duration
    // will be returned.
    // To compute t-d for a duration d, use t.Add(-d).
    func (t Time) Sub(u Time) Duration
    • *

3、转换

1、time.Time 转 int64(时间戳)

    now := time.Now()
    nowInt := now.Unix()

2、int64(时间戳)转time.Time

    // Unix returns t as a Unix time, the number of seconds elapsed
    // since January 1, 1970 UTC. The result does not depend on the
    // location associated with t.
     nowTimeFromInt := time.Unix(nowInt, 0)

3、string 转time.Time

    now := time.Now()
    nowStr := now.Format(time.UnixDate)
    nowTime, err := time.Parse(LayOut, nowStr) //如果转换错误会返回一个err
    • *

4、格式化

1、格式化写法

后面的时间格式"2006-01-02 15:04:05"是固定要这么写的, 🐷 🚗

     fmt.Println(time.Now().Format("2006-01-02 15:04:05"))  

2、格式化常用格式

    const (
        ANSIC       = "Mon Jan _2 15:04:05 2006"
        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
        RFC822      = "02 Jan 06 15:04 MST"
        RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // Handy time stamps.
        Stamp      = "Jan _2 15:04:05"
        StampMilli = "Jan _2 15:04:05.000"
        StampMicro = "Jan _2 15:04:05.000000"
        StampNano  = "Jan _2 15:04:05.000000000"
    )
    • *

5、定时器

1、源码

    // NewTicker returns a new Ticker containing a channel that will send the
    // time with a period specified by the duration argument.
    // It adjusts the intervals or drops ticks to make up for slow receivers.
    // The duration d must be greater than zero; if not, NewTicker will panic.
    // Stop the ticker to release associated resources.
    func NewTicker(d Duration) *Ticker

2、示例

    ticker := time.NewTicker(time.Second)    //定义一个秒级的定时器
    defer ticker.Stop()    //返回时定时器停止
    
    done := make(chan bool)
   
       //业务逻辑 
    go func() {
        time.Sleep(10 * time.Second)
        done <- true
    }()
    
    //每隔一个定时器时间(time.Second),打印一次,直到业务结束
    for {
        select {
        case <-done:
            fmt.Println("Done!")
            return
        case t := <-ticker.C:
            fmt.Println("Current time: ", t)
        }
    }

原网址: 访问
创建于: 2022-08-21 21:35:03
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论