在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

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

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

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.

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)
    }
}