The yday
attribute in Ruby is used to return the day of the year. There are 365 days (366 days for leap years) in a year. So, this method returns an integer that represents the day of the year, from 1 to 366.
time_obj.yday
This attribute returns an integer from 1 to 365 (366 for leap years).
# create time objectstime_now = Time.now # current timetime_five_hours = Time.at(946702800)time_year_2021 = Time.new(2021)time_wednesday_1993 = Time.local(1993, 2, 24)# call the yday method# and print resultsputs time_now.ydayputs time_five_hours.ydayputs time_year_2021.ydayputs time_wednesday_1993.yday
Time.now
attribute that returns the current time object.Time.at()
method to create a time object by passing the number of seconds since the UNIX epoch.Time.new()
method to create a new time object by passing it the year 2021.Time.local()
method to create a time object by passing in the year, month, and day of the month.yday
attribute on the time objects we created and print their results to the console.