Rounding an NSDate down

  • I cannot seem to figure out any way to round down a date without a lot
    of messy conversion to NSCalendarDates and NSStrings and back to an
    NSDate, which seems like it would be error prone.

    If I have a date like this:

    01/01/2007 12:30 PM

    I want to round it down to the nearest hour, basically just dropping
    the minutes so it ends up like this:

    01/01/2007 12:00 PM

    Does anyone know of an easy way to do this?

    Todd Ransom
    Return Self Software
    http://returnself.com
  • On Dec 7, 2007, at 7:42 PM, Todd Ransom wrote:

    > I cannot seem to figure out any way to round down a date without a
    > lot of messy conversion to NSCalendarDates and NSStrings and back to
    > an NSDate, which seems like it would be error prone.
    >
    > If I have a date like this:
    >
    > 01/01/2007 12:30 PM
    >
    > I want to round it down to the nearest hour, basically just dropping
    > the minutes so it ends up like this:
    >
    > 01/01/2007 12:00 PM
    >
    > Does anyone know of an easy way to do this?

    This might work (warning, written in Mail, untested, use at your own
    risk, etc.):

    NSDate *originalDate;    <-- assume this variable has already been
    initialized with the date
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dc = [cal components:NSEraCalendarUnit |
    NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:originalDate];
    NSDate *newDate;

    [dc setMinute:0];
    newDate = [cal dateFromComponents:dc];

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • You shouldn't need to convert to an NSString. NSCalendarDates are
    NSDates, so you should just be able to pass in an NSCalendarDate
    wherever an NSDate is requested.

    //broken up into multiple ops for clarity
    NSCalendarDate* calendarDate = [myDate dateWithCalendarFormat:nil timeZone:nil];
    NSCalendarDate* roundedDate = [calendarDate dateByAddingYears: 0
        months: 0
        days: 0
        hours: 0
        minutes: - [calendarDate minuteOfHour]
        seconds: 0];

    [someObject doNeatThingWithDate:roundedDate];

    -Jim.....

    On Dec 7, 2007 8:42 PM, Todd Ransom <toddransom...> wrote:
    > I cannot seem to figure out any way to round down a date without a lot
    > of messy conversion to NSCalendarDates and NSStrings and back to an
    > NSDate, which seems like it would be error prone.
    >
    > If I have a date like this:
    >
    > 01/01/2007 12:30 PM
    >
    > I want to round it down to the nearest hour, basically just dropping
    > the minutes so it ends up like this:
    >
    > 01/01/2007 12:00 PM
    >
    > Does anyone know of an easy way to do this?
    >
    > Todd Ransom
    > Return Self Software
    > http://returnself.com
    >
  • Thanks Jim and Nick for the responses. I knew it had to be easier than
    what I was trying...

    Todd Ransom
    Return Self Software
    http://returnself.com

    On Dec 7, 2007, at 10:12 PM, Jim Thomason wrote:

    > You shouldn't need to convert to an NSString. NSCalendarDates are
    > NSDates, so you should just be able to pass in an NSCalendarDate
    > wherever an NSDate is requested.
    >
    > //broken up into multiple ops for clarity
    > NSCalendarDate* calendarDate = [myDate dateWithCalendarFormat:nil
    > timeZone:nil];
    > NSCalendarDate* roundedDate = [calendarDate dateByAddingYears: 0
    > months: 0
    > days: 0
    > hours: 0
    > minutes: - [calendarDate minuteOfHour]
    > seconds: 0];
    >
    > [someObject doNeatThingWithDate:roundedDate];
    >
    > -Jim.....
    >
    > On Dec 7, 2007 8:42 PM, Todd Ransom <toddransom...> wrote:
    >> I cannot seem to figure out any way to round down a date without a
    >> lot
    >> of messy conversion to NSCalendarDates and NSStrings and back to an
    >> NSDate, which seems like it would be error prone.
    >>
    >> If I have a date like this:
    >>
    >> 01/01/2007 12:30 PM
    >>
    >> I want to round it down to the nearest hour, basically just dropping
    >> the minutes so it ends up like this:
    >>
    >> 01/01/2007 12:00 PM
    >>
    >> Does anyone know of an easy way to do this?
    >>
    >> Todd Ransom
    >> Return Self Software
    >> http://returnself.com
    >>
  • .. you could also drop down to rounding the date's timestmap.
    timestamp = date.timestemap
    //round timestamp
    date = nsdate dateWithTimestamo:timestmap

    Regards,
    Dominik

    Am Dec 8, 2007 um 4:41 AM schrieb Todd Ransom:

    > Thanks Jim and Nick for the responses. I knew it had to be easier
    > than what I was trying...
    >
    > Todd Ransom
    > Return Self Software
    > http://returnself.com
    >
    >
    >
    > On Dec 7, 2007, at 10:12 PM, Jim Thomason wrote:
    >
    >> You shouldn't need to convert to an NSString. NSCalendarDates are
    >> NSDates, so you should just be able to pass in an NSCalendarDate
    >> wherever an NSDate is requested.
    >>
    >> //broken up into multiple ops for clarity
    >> NSCalendarDate* calendarDate = [myDate dateWithCalendarFormat:nil
    >> timeZone:nil];
    >> NSCalendarDate* roundedDate = [calendarDate dateByAddingYears: 0
    >> months: 0
    >> days: 0
    >> hours: 0
    >> minutes: - [calendarDate minuteOfHour]
    >> seconds: 0];
    >>
    >> [someObject doNeatThingWithDate:roundedDate];
    >>
    >> -Jim.....
    >>
    >> On Dec 7, 2007 8:42 PM, Todd Ransom <toddransom...> wrote:
    >>> I cannot seem to figure out any way to round down a date without a
    >>> lot
    >>> of messy conversion to NSCalendarDates and NSStrings and back to an
    >>> NSDate, which seems like it would be error prone.
    >>>
    >>> If I have a date like this:
    >>>
    >>> 01/01/2007 12:30 PM
    >>>
    >>> I want to round it down to the nearest hour, basically just dropping
    >>> the minutes so it ends up like this:
    >>>
    >>> 01/01/2007 12:00 PM
    >>>
    >>> Does anyone know of an easy way to do this?
    >>>
    >>> Todd Ransom
    >>> Return Self Software
    >>> http://returnself.com
    >>>

previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today