#I have an ordinal date string and I want to change it to an ordinary #date string. For example: 2005/1 should give January 1, 2005. I see #ordinal in the Date class but I need a simple example. Thanks,
i hope this is simple enough.
# we want to get the year and day values fr your string
re = /(\d+)\/(\d+)/ => /(\d+)\/(\d+)/
rs = "2005/1" => "2005/1"
y,d=re.match(rs)[1..2] => ["2005", "1"]
# we need to convert them to integers since ordinal wants num
y = y.to_i => 2005
d = d.to_i => 1
od = Date.ordinal(y,d) => #<Date: 4906743/2,0,2299161>
# we got a date object
# fr here we can do anything
Botp, Todd,
Thanks. Since I had already parsed the ordinal date's year and day to get
them in string sortable order, I just had to take your examples and do this:
require 'date' ... od = Date.ordinal (strYear.to_i, strOrdn.to_i) and then
od.to_s gives the sortable (and readable) yyyy-mm-dd string.
Wow, Ruby is powerful, but the documentation on Date (and perhaps other gem
filled libraries) is hard for a beginner to grok. I found Date's ordinal
with a Google search, which gave me the built-in documentation, but no
examples. So, if I need at least the APIs for a library such as Date I use
"ri"?
Thanks again,
Dave
PS Here's the beginner's way I used to parse the ordinal date string (eg
"2005/17"):
On 11/18/05, Peña, Botp <botp@delmonte-phil.com> wrote:
davyb [mailto:david.a.boyd@gmail.com]
#I have an ordinal date string and I want to change it to an ordinary #date string. For example: 2005/1 should give January 1, 2005. I see #ordinal in the Date class but I need a simple example. Thanks,
i hope this is simple enough.
# we want to get the year and day values fr your string
Yeah, it's backwards in the PS -- just noticed it. (str.reverse won't
help!!!)
···
On 11/18/05, David Boyd <david.a.boyd@gmail.com> wrote:
Botp, Todd,
Thanks. Since I had already parsed the ordinal date's year and day to get
them in string sortable order, I just had to take your examples and do this:
require 'date' ... od = Date.ordinal (strYear.to_i, strOrdn.to_i ) and
then od.to_s gives the sortable (and readable) yyyy-mm-dd string.
Wow, Ruby is powerful, but the documentation on Date (and perhaps other
gem filled libraries) is hard for a beginner to grok. I found Date's ordinal
with a Google search, which gave me the built-in documentation, but no
examples. So, if I need at least the APIs for a library such as Date I use
"ri"?
Thanks again,
Dave
PS Here's the beginner's way I used to parse the ordinal date string (eg
"2005/17"):
strYear = strDate[strDate.index("/")+1, strDate.length]
strOrdn = strDate[0, strDate.index("/")]
On 11/18/05, Peña, Botp <botp@delmonte-phil.com> wrote:
>
> davyb [mailto:david.a.boyd@gmail.com]
>
> #I have an ordinal date string and I want to change it to an ordinary
> #date string. For example: 2005/1 should give January 1, 2005. I see
> #ordinal in the Date class but I need a simple example. Thanks,
>
> i hope this is simple enough.
>
> # we want to get the year and day values fr your string
>
> > re = /(\d+)\/(\d+)/ => /(\d+)\/(\d+)/
> > rs = "2005/1" => "2005/1"
>
> > y,d=re.match(rs)[1..2] => ["2005", "1"]
>
> # we need to convert them to integers since ordinal wants num
>
> > y = y.to_i => 2005
> > d = d.to_i => 1
>
> > od = Date.ordinal(y,d) => #<Date: 4906743/2,0,2299161>
>
> # we got a date object
> # fr here we can do anything
>
> > od.year => 2005
> > od.month => 1
> > od.day => 1
> > nd_str1 = od.to_s => "2005-01-01"
> > nd_str2 = od.strftime("%B %e, %Y") => "January 1, 2005"
> > nd_str3 = nd_str2.squeeze(" ") => "January 1, 2005"
>
> hth.
>
> kind regards
> -botp
>
> #Dave
>
>
Botp, Todd,
Thanks. Since I had already parsed the ordinal date's year and day to get
them in string sortable order, I just had to take your examples and do this:
require 'date' ... od = Date.ordinal (strYear.to_i, strOrdn.to_i) and then
od.to_s gives the sortable (and readable) yyyy-mm-dd string.
Wow, Ruby is powerful, but the documentation on Date (and perhaps other gem
filled libraries) is hard for a beginner to grok. I found Date's ordinal
with a Google search, which gave me the built-in documentation, but no
examples. So, if I need at least the APIs for a library such as Date I use
"ri"?
Yeah, no examples. I guess they just expect us to learn it from them
directly
Oh, and strftime doesn't show up in the online RDoc documentation
(http://www.ruby.org/core). Sometimes, stuff like this gets a little
frustrating so I end up using <Class>.public_methods or
<Class>.instance_methods to find out what's cooking underneath the
hood. I understand keeping docs up to date for a relatively volatile
language like Ruby is difficult.
It does say at the top of the Date doc:
See the documentation to the file date.rb for an overview.
This is on the same page, upper left frame, called lib/date.rb. There
are a couple examples there.
ri doesn't work on my machine for some reason. I probably just have to
reinstall it.
On 11/18/05, William James <w_a_x_man@yahoo.com> wrote:
David Boyd wrote:
> PS Here's the beginner's way I used to parse the ordinal date string (eg
> "2005/17"):
>
> strYear = strDate[strDate.index("/")+1, strDate.length]
> strOrdn = strDate[0, strDate.index("/")]