1

I need to get the end of the current calendar month in rubymotion as a Time object.

So for the month of October 2012, given the current time I'd need October 31, 2012 Midnight as an instance of Time, regardless of the current day.

How do I do this?

EDIT

I appreciate the answers, but one thing I neglected to mention - sorry - is that I am using RubyMotion and the Date and DateTime objects are not available.

Basically, anything that you load with require in ruby, I do not have access to.

silasjmatson
  • 1,814
  • 18
  • 37
  • Check out: http://stackoverflow.com/questions/1489826/how-to-get-the-number-of-days-in-a-given-month-in-ruby-accounting-for-year – sunnyrjuneja Oct 10 '12 at 21:53
  • I don't need the number of days in the month, I need to get a Time object for the end of the calendar month given the current time. – silasjmatson Oct 10 '12 at 22:00

4 Answers4

2

Since you're using RubyMotion you have access to all the iOS SDKs:

NSDate *curDate = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components
    // set last of month
    [comps setMonth:[comps month]+1];
    [comps setDay:0];
    NSDate *tDateMonth = [calendar dateFromComponents:comps];
    NSLog(@"%@", tDateMonth);

Found at Getting the last day of a month

Translation to RubyMotion:

    curDate = NSDate.date
    calendar = NSCalendar.currentCalendar

    # Get necessary date components
    comps = calendar.components(NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit, fromDate:curDate)

    # set last of month
    comps.month += 1
    comps.day = 0
    tDateMonth = calendar.dateFromComponents(comps)
    NSLog("%@", tDateMonth)
Community
  • 1
  • 1
sunnyrjuneja
  • 6,033
  • 2
  • 32
  • 51
1

I think this should do the trick..

require 'date'
(DateTime.now.next_month - DateTime.now.day).to_time

Example:

ruby-1.9.3-p194 :001 > require 'date'
 => true 

ruby-1.9.3-p194 :02 > DateTime.now
 => #<DateTime: 2012-10-10T17:18:15-05:00 ((2456211j,80295s,284081000n),-18000s,2299161j)> 

ruby-1.9.3-p194 :03 > DateTime.now.next_month - DateTime.now.day
 => #<DateTime: 2012-10-31T17:18:16-05:00 ((2456232j,80296s,819683000n),-18000s,2299161j)> 

ruby-1.9.3-p194 :04 > (DateTime.now.next_month - DateTime.now.day).to_time
 => 2012-10-31 17:18:18 -0500 
Adam Eberlin
  • 14,005
  • 5
  • 37
  • 49
0

Ruby on Rails method

Time.now.at_end_of_month
alex
  • 3,682
  • 3
  • 21
  • 22
0

You could increment the "start" datetime until it rolls to the next month:

require 'date'

def last_day_of_month(date=DateTime.now)
  month = date.month
  date += 1 while month == (date + 1).month
  date.to_time
end

last_day_of_month # => 2012-10-31 16:17:21 -0600
nov_1_2010 = DateTime.parse('2010-11-01T01:01:01-0700')
last_day_of_month(nov_1_2010) # => 2010-11-30 01:01:01 -0700 
maerics
  • 151,642
  • 46
  • 269
  • 291