Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the day of year in ruby?

Tags:

What is the best way to get the day of the year for any specific date in Ruby?

For example: 31/dec/2009 should return day 365, and 01/feb/2008 should return day 32

like image 803
Daniel Cukier Avatar asked Nov 19 '09 15:11

Daniel Cukier


People also ask

What is MJD in Ruby?

Modified Julian Day Number¶ ↑ Its days begin at midnight on local time. In this document, when the term “modified Julian day number” simply appears, it just refers to “chronological modified Julian day number”, not the original. In those classes, those are so-called “amjd” and “mjd”.

How do you create a Date object in Ruby?

A Date object is created with Date::new , Date::jd , Date::ordinal , Date::commercial , Date::parse , Date::strptime , Date::today , Time#to_date , etc. require 'date' Date. new(2001,2,3) #=> #<Date: 2001-02-03 ...> Date.

How do you parse time in Ruby?

Ruby | DateTime parse() function DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object. Return: given representation of date and time, and creates a DateTime object.


1 Answers

Basically (shown here in irb):

>> require 'date'  >> Date.today.to_s => "2009-11-19"  >> Date.today.yday() => 323 

For any date:

>> Date.new(y=2009,m=12,d=31).yday => 365 

Or:

>> Date.new(2012,12,31).yday => 366 

@see also: Ruby Documentation

like image 142
miku Avatar answered Sep 22 '22 11:09

miku