How to create a date range in the format of YYYY-MM-DD from start to end in Golang?

huangapple go评论97阅读模式
英文:

How to create a date range in the format of YYYY-MM-DD from start to end in Golang?

问题

假设我们的输入是 start_date=2022-01-01end_date=2022-01-05。我该如何得到以下形式的输入:

2022-01-01
2022-01-02
2022-01-03
2022-01-04

我可以使用 time.Parse 解析起始日期和结束日期,使用 .Sub 获取它们之间的天数,然后遍历这个范围并创建字符串日期。我想知道在 Go 中是否有任何方法或更好的解决方案来创建日期范围?

英文:

Let's say our inputs are start_date=2022-01-01 and end_date=2022-01-05. How I can get an input like below:

2022-01-01
2022-01-02
2022-01-03
2022-01-04

I'm able to parse start and end using time.Parse and get the days in between using .Sub and then iterating over the range and create string dates.
I was wondering if there is any method or better solution for date range creation in Go?

答案1

得分: 1

你可以使用以下代码:

const (
    layout = "2006-01-02"
)

func main() {
    startDate, _ := time.Parse(layout, "2022-01-01")
    endDate, _ := time.Parse(layout, "2022-01-05")

    for date := startDate; date.Before(endDate); date = date.AddDate(0, 0, 1) {
        fmt.Println(date.Format(layout))
    }
}

这将输出:

2022-01-01
2022-01-02
2022-01-03
2022-01-04

完整示例

英文:

You can use:

const (
	layout = "2006-01-02"
)

func main() {
	startDate, _ := time.Parse(layout, "2022-01-01")
	endDate, _ := time.Parse(layout, "2022-01-05")

	for date := startDate; date.Before(endDate); date = date.AddDate(0, 0, 1) {
		fmt.Println(date.Format(layout))
	}
}

This will give you:

2022-01-01
2022-01-02
2022-01-03
2022-01-04

Full example

答案2

得分: 0

Go语言不使用yyyy-mm-dd的格式来格式化时间。相反,你需要使用一个特殊的格式参数来格式化时间或日期。(当以01/02 03:04:05PM '06 -0700的形式书写时,这个日期更容易记住。)

const (
    layoutISO = "2006-01-02"
    layoutUS  = "January 2, 2006"
)
date := "1999-12-31"
t, _ := time.Parse(layoutISO, date)
fmt.Println(t)                  // 1999-12-31 00:00:00 +0000 UTC
fmt.Println(t.Format(layoutUS)) // December 31, 1999

函数time.Parse用于解析日期字符串,而Format用于格式化time.Time对象。它们的签名如下:

func Parse(layout, value string) (Time, error)
func (t Time) Format(layout string) string

你可以使用以下代码来处理日期范围:

const (
    layoutISO = "2006-01-02"
    layoutUS  = "January 2, 2006"
)

func main() {
    date := "1999-12-31"
    t, _ := time.Parse(layoutISO, date)
    fmt.Println(t)                  // 1999-12-31 00:00:00 +0000 UTC
    fmt.Println(t.Format(layoutUS)) // December 31, 1999
    
    startDate, _ := time.Parse(layoutISO, "2022-01-01")
    endDate, _ := time.Parse(layoutISO, "2022-01-05")
    for date := startDate; date.Before(endDate); date = date.AddDate(0, 0, 1) {
        fmt.Println(date.Format(layoutISO))
    }
}
英文:

Go doesn’t use yyyy-mm-dd layout to format a time. Instead, you format a special layout parameter

Mon Jan 2 15:04:05 MST 2006

the same way as the time or date should be formatted. (This date is easier to remember when written as 01/02 03:04:05PM ‘06 -0700.)

const (
	layoutISO = "2006-01-02"
	layoutUS  = "January 2, 2006"
)
date := "1999-12-31"
t, _ := time.Parse(layoutISO, date)
fmt.Println(t)                  // 1999-12-31 00:00:00 +0000 UTC
fmt.Println(t.Format(layoutUS)) // December 31, 1999

The function

  • time.Parse parses a date string, and

  • Format formats a time.Time.

They have the following signatures:

 func Parse(layout, value string) (Time, error)
  func (t Time) Format(layout string) string

you can use this for date range

const (
	layoutISO = "2006-01-02"
	layoutUS  = "January 2, 2006"
)

func main() {
	date := "1999-12-31"
	t, _ := time.Parse(layoutISO, date)
	fmt.Println(t)                  // 1999-12-31 00:00:00 +0000 UTC
	fmt.Println(t.Format(layoutUS)) // December 31, 1999
	
	
	startDate, _ := time.Parse(layoutISO, "2022-01-01")
	endDate, _ := time.Parse(layoutISO, "2022-01-05")
	for date := startDate; date.Before(endDate); date = date.AddDate(0, 0, 1) {
		fmt.Println(date.Format(layoutISO))
	}

}

huangapple
  • 本文由 发表于 2022年12月19日 15:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/74847181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定