Re: Rounding an NSDate down
Re: Rounding an NSDate down
- Subject: Re: Rounding an NSDate down
- From: Nick Zitzmann <email@hidden>
- Date: Fri, 7 Dec 2007 20:11:57 -0700
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/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden