作者:aGreadyCat__895 | 来源:互联网 | 2023-08-17 19:06
HowcanIgetcurrentyearinRubyonRails?如何在RubyonRails中获得当前年份?Itriedavarietyofthings,
How can I get current year in Ruby on Rails?
如何在Ruby on Rails中获得当前年份?
I tried a variety of things, including
我尝试过各种各样的东西,包括
- Date.current.year
- Time.now.year
The problem is they return the previous year in cases where year changes after launching the server (eg. after new year's).
问题是,如果在启动服务器之后年份发生变化(例如新年之后),它们会在前一年返回。
Relevant code:
Model brewery.rb
class Brewery
Problem occurs when creating a new brewery, so I assumed Date.today.year would be evaluated whenever that action takes place.
创建新啤酒厂时会出现问题,因此我假设每当该操作发生时都会评估Date.today.year。
4 个解决方案