Arun Kaushik
Arun Kaushik

Reputation: 19

Rails, day of year to date

I have day of the year. For example 15th day of 2014, or 210th day of 2014. I need to get the Date on the particular day. Is there any library function in Rails/Ruby that I can use, or any other elegant way?

Something like:

15th day of 2014 = 15-Jan-2014

210th day of 2014 = 29-Jul-2014

Upvotes: 1

Views: 1127

Answers (2)

wonderb0lt
wonderb0lt

Reputation: 2043

You can use Date.ordinal for that:

require 'date'

Date.ordinal(2014, 210)
# => #<Date: 2014-07-29 ((2456868j,0s,0n),+0s,2299161j)>

Upvotes: 6

Andy Waite
Andy Waite

Reputation: 11076

Date.new(2014, 1, 1) + 210
=> Wed, 30 Jul 2014

Edit - you'd need to subtract 1 day. I prefer @wonderb0lt's suggestion.

Upvotes: 0

Related Questions