Re: Current Hour & Minutes
Re: Current Hour & Minutes
- Subject: Re: Current Hour & Minutes
- From: Scott Stevenson <email@hidden>
- Date: Sun, 8 Apr 2007 15:13:03 -0700
On Apr 8, 2007, at 1:34 PM, Nick Zitzmann wrote:
NSCalendarDate *now = [NSCalendarDate date];
int hour = [now hourOfDay];
int minute = [now minuteOfHour];
Alternately, this will work in Tiger and later, and is probably
more future-proof:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *hourAndMinute = [cal
components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:
[NSDate date]];
int hour = [hourAndMinute hour];
int minute = [hourAndMinute minute];
For what it's worth, THCalendarInfo (which the original poster was
using) already uses CFCalendar, which should be future-proof:
+ (int) currentHourIn24HourFormat
{
CFCalendarRef myCalendar = CFCalendarCopyCurrent();
int myHour = CFCalendarGetOrdinalityOfUnit (
myCalendar,
kCFCalendarUnitHour,
kCFCalendarUnitDay,
[self currentAbsoluteTime]
);
// adjust for real-world expectations
// otherwise, 3:45 is '4' (fourth hour)
myHour--;
if ( myCalendar ) CFRelease( myCalendar );
return myHour;
}
- Scott
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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