Retrieving current local time on iPhone?

后端 未结 7 560
我寻月下人不归
我寻月下人不归 2020-12-01 07:57

I\'m looking to get the current hour and minute on a user\'s iPhone for display in an app that doesn\'t show the status bar. Is there a simple way to do this?

相关标签:
7条回答
  • 2020-12-01 08:27
    -(void)currentTime
    {    
        //Get current time
        NSDate* now = [NSDate date];
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *dateComponents = [gregorian components:(NSCalendarUnitHour  | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:now];
        NSInteger hour = [dateComponents hour];
        NSString *am_OR_pm=@"AM";
    
        if (hour>12)
        {
            hour=hour%12;
    
            am_OR_pm = @"PM";
        }
    
        NSInteger minute = [dateComponents minute];
        NSInteger second = [dateComponents second];
        [gregorian release];
    
        NSLog(@"Current Time  %@",[NSString stringWithFormat:@"%02ld:%02ld:%02ld %@", (long)hour, (long)minute, (long)second,am_OR_pm]);
    }
    
    0 讨论(0)
提交回复
热议问题