开发者

Finding the time in iphone programming

I need to find the current time for my iphone app. I need it broken up into the differ开发者_如何学Cent units, so I need the hour, minute, and second in different variables. Please help!


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];


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


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH"];
int hour = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter setDateFormat:@"mm"];
int minute = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter setDateFormat:@"ss"];
int second = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜