Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the time in iphone programming

I need to find the current time for my iphone app. I need it broken up into the different units, so I need the hour, minute, and second in different variables. Please help!

like image 754
Clayton Avatar asked Apr 23 '26 21:04

Clayton


2 Answers

NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

[calendar release];
like image 91
bryanmac Avatar answered Apr 25 '26 12:04

bryanmac


NSDate           *date           = [NSDate date];
NSCalendar       *calendar       = [NSCalendar currentCalendar];
NSCalendarUnit    unitFlags      = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

NSInteger hour   = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

NSLog(@"%ld:%ld:%ld", hour, minute, second); 

23:34:6

like image 22
zaph Avatar answered Apr 25 '26 11:04

zaph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!