golang - 枚举月份中的的所有天(包括起始时间和终止时间)

go-bot

2018-05-23T08:01:55Z||2018-05-23T08:01:55Z

代码:

package main

import (
    "fmt"
    "log"
    "time"
)

func main() {
    // *** Iterate through each days of Feb. 2020 (2020 is a leap year) 
    // Convert "2020-2" to time.Time
    // Note that the “2006-1” is the layout parameter, see https://golang.org/pkg/time/#Parse
    month, err := time.Parse("2006-1", "2020-2")
    if err != nil {
        log.Fatal(err)
    }

    // Iterate through each day of the month
    for day := month; day.Month() == month.Month(); day = day.AddDate(0, 0, 1) {
        // Get end of the day
        dayEnd := day.AddDate(0, 0, 1).Add(-time.Nanosecond)
        fmt.Printf("%v - %v\n", day, dayEnd)
    }
}

输出:

2020-02-01 00:00:00 +0000 UTC - 2020-02-01 23:59:59.999999999 +0000 UTC
2020-02-02 00:00:00 +0000 UTC - 2020-02-02 23:59:59.999999999 +0000 UTC
2020-02-03 00:00:00 +0000 UTC - 2020-02-03 23:59:59.999999999 +0000 UTC
2020-02-04 00:00:00 +0000 UTC - 2020-02-04 23:59:59.999999999 +0000 UTC
2020-02-05 00:00:00 +0000 UTC - 2020-02-05 23:59:59.999999999 +0000 UTC
2020-02-06 00:00:00 +0000 UTC - 2020-02-06 23:59:59.999999999 +0000 UTC
2020-02-07 00:00:00 +0000 UTC - 2020-02-07 23:59:59.999999999 +0000 UTC
2020-02-08 00:00:00 +0000 UTC - 2020-02-08 23:59:59.999999999 +0000 UTC
2020-02-09 00:00:00 +0000 UTC - 2020-02-09 23:59:59.999999999 +0000 UTC
2020-02-10 00:00:00 +0000 UTC - 2020-02-10 23:59:59.999999999 +0000 UTC
2020-02-11 00:00:00 +0000 UTC - 2020-02-11 23:59:59.999999999 +0000 UTC
2020-02-12 00:00:00 +0000 UTC - 2020-02-12 23:59:59.999999999 +0000 UTC
2020-02-13 00:00:00 +0000 UTC - 2020-02-13 23:59:59.999999999 +0000 UTC
2020-02-14 00:00:00 +0000 UTC - 2020-02-14 23:59:59.999999999 +0000 UTC
2020-02-15 00:00:00 +0000 UTC - 2020-02-15 23:59:59.999999999 +0000 UTC
2020-02-16 00:00:00 +0000 UTC - 2020-02-16 23:59:59.999999999 +0000 UTC
2020-02-17 00:00:00 +0000 UTC - 2020-02-17 23:59:59.999999999 +0000 UTC
2020-02-18 00:00:00 +0000 UTC - 2020-02-18 23:59:59.999999999 +0000 UTC
2020-02-19 00:00:00 +0000 UTC - 2020-02-19 23:59:59.999999999 +0000 UTC
2020-02-20 00:00:00 +0000 UTC - 2020-02-20 23:59:59.999999999 +0000 UTC
2020-02-21 00:00:00 +0000 UTC - 2020-02-21 23:59:59.999999999 +0000 UTC
2020-02-22 00:00:00 +0000 UTC - 2020-02-22 23:59:59.999999999 +0000 UTC
2020-02-23 00:00:00 +0000 UTC - 2020-02-23 23:59:59.999999999 +0000 UTC
2020-02-24 00:00:00 +0000 UTC - 2020-02-24 23:59:59.999999999 +0000 UTC
2020-02-25 00:00:00 +0000 UTC - 2020-02-25 23:59:59.999999999 +0000 UTC
2020-02-26 00:00:00 +0000 UTC - 2020-02-26 23:59:59.999999999 +0000 UTC
2020-02-27 00:00:00 +0000 UTC - 2020-02-27 23:59:59.999999999 +0000 UTC
2020-02-28 00:00:00 +0000 UTC - 2020-02-28 23:59:59.999999999 +0000 UTC
2020-02-29 00:00:00 +0000 UTC - 2020-02-29 23:59:59.999999999 +0000 UTC