7.9. Finding the Day of the Year

The day number within the year is sometimes called the Julian date, which is not directly related to the Julian calendar, which has gone out of style. Other people insist that this usage is not correct, so we won’t use it from here on.

No matter what you call it, there will be times you want to know what day of the year it is, from 1 to 366. This is easy in Ruby; we use the yday method:

t = Time.now
day = t.yday      # 315

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.