# Calendar events

**URL:** <https://www.ruby-forum.com/t/calendar-events/113572>\
**Category:** Ruby\
**Created:** [September 2, 2007, 1:56am UTC](https://www.ruby-forum.com/t/calendar-events/113572 "2007-09-02T01:56:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 2, 2007, 1:56am UTC](https://www.ruby-forum.com/t/calendar-events/113572/1 "2007-09-02T01:56:36Z")

</div>

i want a simply get the amount of days inbetween a range of dates. IE:  
1/25 - 2/3 is 8 days difference.

My initial approach the the problem was to create a huge variable setup:

jan = 31 #31 = days in january  
feb = 28  
mar = 31  
apr = 30

im sure thers a better and cleaner way, any ideas on how to approach  
this?

Thanks

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 2, 2007, 2:10am UTC](https://www.ruby-forum.com/t/calendar-events/113572/2 "2007-09-02T02:10:06Z")

</div>

> ## My initial approach the the problem was to create a huge Thanks
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

There are standard libraries for this:

irb(main):001:0\> require ‘date’  
=\> true  
irb(main):002:0\> puts “February 2nd 2007 and January 25th 2007 are  
#{Date.new(2007, 2, 3) - Date.new(2007, 1, 25)} days apart”  
February 2nd 2007 and January 25th 2007 are 9 days apart  
=\> nil  
irb(main):003:0\>

If you subtract a date object from a date object, the result is “how  
many  
days later the current date is than x.”

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 2, 2007, 2:44am UTC](https://www.ruby-forum.com/t/calendar-events/113572/3 "2007-09-02T02:44:34Z")

</div>

Felix W. wrote:

> > My initial approach the the problem was to create a huge

> If you subtract a date object from a date object, the result is “how  
> many  
> days later the current date is than x.”
> 
> HTH,
> 
> Felix

Perfect. Thanks a ton!

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 2, 2007, 3:35am UTC](https://www.ruby-forum.com/t/calendar-events/113572/4 "2007-09-02T03:35:11Z")

</div>

just another quick question…if i wanted to put a string into a date  
format such as the following:

res = []  
array = [“2/14/2005”,83,35,23]  
res \<\< array.grep(/2/\14/\2005/)

# so now res has “2/14/2005” in it…how would i turn that into a –

specdate = Date.new(2, 24, 2005)

so i need 2/14/2005 turned into a date object, preferably not having to  
use the format of Date.new(year,day,month) …id prefer to have it in  
the format of month,day,year if possible 🙂

much thanks!

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 2, 2007, 3:56am UTC](https://www.ruby-forum.com/t/calendar-events/113572/5 "2007-09-02T03:56:13Z")

</div>

> use the format of Date.new(year,day,month) …id prefer to have it in  
> the format of month,day,year if possible 🙂
> 
> ## much thanks!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Once you have a string containing the date separated by slashes, you can  
split the string on separators to parse the date parts into variables,  
and  
then use those to construct the date object.

irb(main):001:0\> month, day, year = “2/14/2005”.split(“/”).collect{|i|  
Integer(i)}  
=\> [2, 14, 2005]  
irb(main):002:0\>

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 2, 2007, 4:51am UTC](https://www.ruby-forum.com/t/calendar-events/113572/6 "2007-09-02T04:51:16Z")

</div>

t = Time.parse ‘9/1/2007’

On Sep 1, 2007, at 9:55 PM, “Felix W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 2, 2007, 5:23am UTC](https://www.ruby-forum.com/t/calendar-events/113572/7 "2007-09-02T05:23:17Z")

</div>

On Sep 1, 2007, at 8:55 PM, Felix W. wrote:

> > use the format of Date.new(year,day,month) …id prefer to have it in  
> > variables, and  
> > then use those to construct the date object.
> 
> irb(main):001:0\> month, day, year = “2/14/2005”.split("/").collect{|i|  
> Integer(i)}  
> =\> [2, 14, 2005]  
> irb(main):002:0\>

You can go straight into the Date object:

> > require “date”  
> > =\> false
> 
> > date = Date.strptime(“2/14/2005”, “%m/%d/%Y”)  
> > =\> #\<Date: 4906831/2,0,2299161\>
> 
> > date.to\_s  
> > =\> “2005-02-14”

James Edward G. II

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 3, 2007, 3:44am UTC](https://www.ruby-forum.com/t/calendar-events/113572/8 "2007-09-03T03:44:18Z")

</div>

From: Michael L. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# res = []

# array = [“2/14/2005”,83,35,23]

# res \<\< array.grep(/2/\14/\2005/)

# so now res has “2/14/2005” in it…how would i turn that into a –

# specdate = Date.new(2, 24, 2005)

# so i need 2/14/2005 turned into a date object, preferably not

# having to use the format of Date.new(year,day,month)

# …id prefer to have it in

# the format of month,day,year if possible 🙂

hmmm, you already have that copy in array. why not put a date object  
right into res. one you have the date obj, you can format it anyway you  
like

eg,

irb(main):019:0\> res=[]  
=\> []  
irb(main):020:0\> array = [“2/14/2005”,83,35,23]  
=\> [“2/14/2005”, 83, 35, 23]  
irb(main):025:0\> res \<\< Date.strptime(array.first, “%m/%d/%Y”)  
=\> [#\<Date: 4906831/2,0,2299161\>]

now res contains a date object  
it’s now a date object, so you format it w your hearts content…

irb(main):031:0\> res[0].strftime(“%m-%d-%y”)  
=\> “02-14-05”  
irb(main):032:0\> res[0].strftime(“%m/%d/%y”)  
=\> “02/14/05”  
irb(main):039:0\> res[0].strftime(“%m/%d/%Y”)  
=\> “02/14/2005”  
irb(main):040:0\> res[0].strftime(“%m:%d:%Y”)  
=\> “02:14:2005”  
irb(main):041:0\> res[0].strftime(“%m %d %Y”)  
=\> “02 14 2005”  
irb(main):042:0\> res[0].strftime(“\<%m\> \<%d\> \<%Y\>”)  
=\> “\<02\> \<14\> \<2005\>”  
irb(main):043:0\> res[0].strftime(“Month: %m\nDay: %d\nYear: %Y\n”)  
=\> “Month: 02\nDay: 14\nYear: 2005\n”  
irb(main):044:0\> puts res[0].strftime(“Month: %m\nDay: %d\nYear: %Y\n”)  
Month: 02  
Day: 14  
Year: 2005  
=\> nil

kind regards -botp
