jjzjj

datetime - 无法在 Golang 的时间包中遍历年底之后的日子

coder 2024-07-05 原文

我正在尝试遍历 2 个日期之间的天数。一个日期是 time.Parse("2006-01-02", "2017-08-24") 另一个是 time.Now() 但我的循环似乎是在年末停止,我不明白为什么。

func IsToday(date time.Time) bool {
    today := time.Now()
    return date.Year() == today.Year() &&
           date.Month() == today.Month() &&
           date.Day() == date.Day()
}

t1, _ := time.Parse("2006-01-02", "2017-08-24")

for curr := t1; !IsToday(curr); curr = curr.AddDate(0,0,1) {
        fmt.Println(curr)
}

循环打印出:

2017-08-24 00:00:00 +0000 UTC
2017-08-25 00:00:00 +0000 UTC
2017-08-26 00:00:00 +0000 UTC
2017-08-27 00:00:00 +0000 UTC
2017-08-28 00:00:00 +0000 UTC
2017-08-29 00:00:00 +0000 UTC
2017-08-30 00:00:00 +0000 UTC
2017-08-31 00:00:00 +0000 UTC
2017-09-01 00:00:00 +0000 UTC
2017-09-02 00:00:00 +0000 UTC
...
2017-12-18 00:00:00 +0000 UTC
2017-12-19 00:00:00 +0000 UTC
2017-12-20 00:00:00 +0000 UTC
2017-12-21 00:00:00 +0000 UTC
2017-12-22 00:00:00 +0000 UTC
2017-12-23 00:00:00 +0000 UTC
2017-12-24 00:00:00 +0000 UTC
2017-12-25 00:00:00 +0000 UTC
2017-12-26 00:00:00 +0000 UTC
2017-12-27 00:00:00 +0000 UTC
2017-12-28 00:00:00 +0000 UTC
2017-12-29 00:00:00 +0000 UTC
2017-12-30 00:00:00 +0000 UTC
2017-12-31 00:00:00 +0000 UTC

请注意它是如何直到今天才迭代的。将 AddDate 单独测试为 time.Parse("2006-01-02", "2017-12-31").AddDate(0,0,1) 会导致 2018-01 -01

最佳答案

计算明天的日期并在 curr 早于该日期时进行迭代:

now := time.Now()
tomorrow := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, 1)
for curr := t1; curr.Before(tomorrow); curr = curr.AddDate(0, 0, 1) {
    fmt.Println(curr)
}

迭代 while !IsToday(curr)(已更正拼写错误)是脆弱的,因为如果开始时间晚于当前时间,它将永远循环。

关于datetime - 无法在 Golang 的时间包中遍历年底之后的日子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48372947/

有关datetime - 无法在 Golang 的时间包中遍历年底之后的日子的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby-on-rails - Rails 3 I18 : translation missing: da. datetime.distance_in_words.about_x_hours - 2

    我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  8. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  9. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  10. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

随机推荐