为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
慕村225694 2023-06-01 14:26:02
在 Golang 中是否可以通过给定的日期变量增加 for 循环中的日期,直到达到当前日期/时间。现在()// Start datet, _ := time.Parse(time.RFC3339, "2018-07-19T12:25:10.8584224+02:00")// Current datect := 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)}我希望变量 d 打印出所有日期(包括时间等),直到它到达当前日期
查看完整描述

4 回答

?
叮当猫咪

TA贡献1776条经验 获得超12个赞

根据 godoc: https: //golang.org/pkg/time/#Time.Day

func (t Time) Day() int

Day 返回 t 指定的月中的第几天。

所以比较 d.Day() 和 ct.Day() 不是正确的方法。如果今天是“2019-01-01”,你的开始时间是“2018-12-23”怎么办?

比较两个时间的正确方法。时间是https://golang.org/pkg/time/#Time.After

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


After 报告时刻 t 是否在 u 之后。Before 报告时刻 t 是否在 u 之前。


所以@Alex Pliutau 的解决方案更常用。但今天需要更加小心。


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)

    }

}

输出:


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


查看完整回答
反对 回复 2023-06-01
?
ITMISS

TA贡献1871条经验 获得超8个赞

获得正确的循环条件和..


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


查看完整回答
反对 回复 2023-06-01
?
慕运维8079593

TA贡献1876条经验 获得超5个赞

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)

    }

}


查看完整回答
反对 回复 2023-06-01
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

根据您的评论,您实际上需要将Format日期告诉它一些有价值的东西:


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

    }

}

这是您的代码的更简单版本(我使用了自定义时间格式,因为我不想编辑 RFC 语法,但最终它是同一件事)= 为了简洁起见,我也在迭代 Month。


查看完整回答
反对 回复 2023-06-01
  • 4 回答
  • 0 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信