Ruby 将日期戳转换为 Ruby 中的年份
我正在尝试将从数据库中获取的日期戳转换为指示一个人有多少岁的值。我确信这很容易,但我似乎无法弄清楚。
Possible Duplicate:
How to calculate how many years passed since a given date in Ruby?
I am trying to convert a datestamp taken from the database into value indicating how many years old a person is. I am sure this easy, but I can't seem to figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设将日期戳作为日期时间值检索:
birth_date
是您应该从数据库中检索的模拟值。第一个值是年,第二个值是小数年。或者,您可以这样做:
如果此人尚未过生日,则会进行调整。例如,调整生日:
Assuming the datestamp is being retrieved as a DateTime value:
birth_date
is a mock value for what you should be retrieving from your database. The first value is years, the second is fractional years.Alternately, you can do it this way:
This adjusts in case the person hasn't had their birthday yet. For instance, tweaking the birthday: