duanshan3065
2019-01-10 15:49
浏览 34
已采纳

在time.Now()到达时进行循环

Is it possible in Golang to increment a date in a for loop by a given date variable till it reached the current date/ time.Now()

// Start date
t, _ := time.Parse(time.RFC3339, "2018-07-19T12:25:10.8584224+02:00")

// Current date
ct := time.Now()

for d := t; d.Day() == ct.Day(); d = d.AddDate(0, 0, 1) {
    // Print all days between start date and current date 
    fmt.Println(d)
}

I expect that variable d prints out all dates (with time etc.) till it reached the current date

图片转代码服务由CSDN问答提供 功能建议

在Golang中是否有可能通过给定的日期变量将for循环中的日期递增,直到达到当前日期 / time.Now()

  //开始日期
t,_:= time.Parse(time.RFC3339,“ 2018-07-19T12:25:10.8584224 + 02  :00“)
 
 //当前日期
ct:=时间。Now()
 
 for d:= t;  d.Day()== ct.Day();  d = d.AddDate(0,0,1){
 //打印开始日期和当前日期之间的所有日期
 fmt.Println(d)
} 
   
 \  n 

我希望变量d打印出所有日期(包括时间等),直到达到当前日期为止

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doudouxuqh198138 2019-01-16 08:55
    已采纳

    according to godoc: https://golang.org/pkg/time/#Time.Day

    func (t Time) Day() int

    Day returns the day of the month specified by t.

    So comparing d.Day() and ct.Day() is not the right approaches. What if today is "2019-01-01",and you start time is "2018-12-23"?

    The right way to compare two time.Time is https://golang.org/pkg/time/#Time.After

    func (t Time) After(u Time) bool
    func (t Time) Before(u Time) bool
    

    After reports whether the time instant t is after u. Before reports whether the time instant t is before u.

    So @Alex Pliutau's solution is more in common use. But need more careful with today.

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t, _ := time.Parse(time.RFC3339, "2009-11-02T12:25:10.8584224+02:00")
    
        // truncate to 0:0:0
        t = t.Truncate(24 * time.Hour)
        fmt.Println("start time is:", t)
    
        // Current date truncate to 0:0:0
        ct := time.Now().Truncate(24 * time.Hour)
        fmt.Println("now is:", ct)
        fmt.Println("---------------")
    
        // for t.Before(ct) {  //if you don't want to print the date of today
        for !t.After(ct) {
            // Print all days between start date and current date
            fmt.Println(t.Format("2006-01-02 15:04:05"))
            t = t.AddDate(0, 0, 1)
        }
    }
    

    Output:

    start time is: 2009-11-02 02:00:00 +0200 +0200
    now is: 2009-11-10 00:00:00 +0000 UTC
    ---------------
    2009-11-02 02:00:00
    2009-11-03 02:00:00
    2009-11-04 02:00:00
    2009-11-05 02:00:00
    2009-11-06 02:00:00
    2009-11-07 02:00:00
    2009-11-08 02:00:00
    2009-11-09 02:00:00
    2009-11-10 02:00:00
    

    https://play.golang.org/p/iMr7M5W9K4N

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshai8330 2019-01-10 15:54

    get the loop condition right and..

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        fmt.Println("Hello, playground")
        t, _ := time.Parse(time.RFC3339, "2018-07-19T12:25:10.8584224+02:00")
    
        // Current date
        ct := time.Now()
    
        for d := t; d.Day() >= ct.Day(); d = d.AddDate(0, 0, 1) {
            // Print all days between start date and current date
            fmt.Println(d)
        }
    }
    
    Hello, playground
    2018-07-19 12:25:10.8584224 +0200 +0200
    2018-07-20 12:25:10.8584224 +0200 +0200
    2018-07-21 12:25:10.8584224 +0200 +0200
    2018-07-22 12:25:10.8584224 +0200 +0200
    2018-07-23 12:25:10.8584224 +0200 +0200
    2018-07-24 12:25:10.8584224 +0200 +0200
    2018-07-25 12:25:10.8584224 +0200 +0200
    2018-07-26 12:25:10.8584224 +0200 +0200
    2018-07-27 12:25:10.8584224 +0200 +0200
    2018-07-28 12:25:10.8584224 +0200 +0200
    2018-07-29 12:25:10.8584224 +0200 +0200
    2018-07-30 12:25:10.8584224 +0200 +0200
    2018-07-31 12:25:10.8584224 +0200 +0200
    

    https://play.golang.org/p/yRBTUZKfseG

    评论
    解决 无用
    打赏 举报
  • doufu2396 2019-01-10 15:55

    Based on your comments, you need to actually tell it to Format the date to something of value:

    package main
    
    import (
        "fmt"
        "log"
        "time"
    )
    
    func main() {
        start, err := time.Parse("2006-1-2", "2018-1-1")
        if err != nil {
            log.Fatal(err)
        }
    
        for d := start; d.Month() == start.Month(); d = d.AddDate(0, 0, 1) {
            fmt.Println(d.Format("2006-1-2"))
        }
    }
    

    Here's a simpler version of your code (I used a custom time format, cause I didn't wanna edit the RFC syntax, but ultimately it's the same thing) = I'm also iterating Month for brevity.

    评论
    解决 无用
    打赏 举报
  • drgovyk64676 2019-01-10 15:59
    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t, _ := time.Parse(time.RFC3339, "2018-07-19T12:25:10.8584224+02:00")
    
        ct := time.Now()
    
        for t.Before(ct) {
            fmt.Println(t)
            t.AddDate(0, 0, 1)
        }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题