# Re: Ordinal Date String to Standard Date String for a Beginn

**URL:** <https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260>\
**Category:** Ruby\
**Created:** [November 18, 2005, 10:46am UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260 "2005-11-18T10:46:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 18, 2005, 10:46am UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/1 "2005-11-18T10:46:13Z")

</div>

davyb [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

#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

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 18, 2005, 11:58am UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/2 "2005-11-18T11:58:46Z")

</div>

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("/")]

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 18, 2005, 12:01pm UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/3 "2005-11-18T12:01:46Z")

</div>

Yeah, it’s backwards in the PS – just noticed it. (str.reverse won’t  
help!!!)

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 18, 2005, 7:59pm UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/4 "2005-11-18T19:59:36Z")

</div>

David Boyd 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”?

Yeah, no examples. I guess they just expect us to learn it from them  
directly 🙂

Todd

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 18, 2005, 8:08pm UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/5 "2005-11-18T20:08:39Z")

</div>

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("/")]

strYear, strOrdn = “2005/17”.split("/")

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 18, 2005, 8:14pm UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/6 "2005-11-18T20:14:42Z")

</div>

Oh, and strftime doesn’t show up in the online RDoc documentation  
([http://www.ruby.org/core](http://www.ruby.org/core)). Sometimes, stuff like this gets a little  
frustrating so I end up using .public\_methods or  
.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.

Todd

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 18, 2005, 8:32pm UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/7 "2005-11-18T20:32:48Z")

</div>

On Sat, 19 Nov 2005, Todd wrote:

> Oh, and strftime doesn’t show up in the online RDoc documentation  
> ([http://www.ruby.org/core](http://www.ruby.org/core)). Sometimes, stuff like this gets a little

[http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000404](http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000404)  
[http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/DateTime.html#M000416](http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/DateTime.html#M000416)

```
    Hugh

```

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 20, 2005, 9:38am UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/8 "2005-11-20T09:38:30Z")

</div>

How elegant! Thanks!  
Dave

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 18, 2005, 10:09pm UTC](https://www.ruby-forum.com/t/re-ordinal-date-string-to-standard-date-string-for-a-beginn/48260/9 "2005-11-18T22:09:23Z")

</div>

Hugh S. wrote:

> On Sat, 19 Nov 2005, Todd wrote:
> 
> > Oh, and strftime doesn’t show up in the online RDoc documentation  
> > ([http://www.ruby.org/core](http://www.ruby.org/core)). Sometimes, stuff like this gets a little
> 
> [http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000404](http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000404)  
> [http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/DateTime.html#M000416](http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/DateTime.html#M000416)
> 
> ```
> Hugh
> 
> ```

Oops, I meant [RDoc Documentation](http://www.ruby-doc.org/core)

So, I guess I should really be looking at the Standard Library API  
instead of the Core API link from now on?

Todd
