开发者

How can I create DateTime object based on the day?

开发者 https://www.devze.com 2023-03-17 10:53 出处:网络
I want to cr开发者_开发知识库eate an Datetime object based on the number of day in the year. This number is from the 365 days of the year (for example it can be: 123 or 23 or 344...)

I want to cr开发者_开发知识库eate an Datetime object based on the number of day in the year. This number is from the 365 days of the year (for example it can be: 123 or 23 or 344...)

How can I do that?

Thanks


Use the DateTime.ordinal method. Here's an example to get the 100th day of year 2011.

require 'date'
year, day = 2011, 100
DateTime.ordinal(year, day)
# #<DateTime: 2011-04-10T00:00:00+00:00 (4911323/2,0,2299161)>


If you want it as the number of days from now you should do the following:

time = Time.new + (60*60*24)*(numberOfDaysFromNow)

If you want it as the number of days from the start of the year you should do the following

time = Time.new(Time.now.year) + (60*60*24)*(dayOfTheYear-1)
0

精彩评论

暂无评论...
验证码 换一张
取 消