[Code Note] iOS-Get current calendar time, note ios-Get calendar
I. Code.
-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. NSLog (@ "------- getCurrentTime --- % @", [self getCurrentTime]);} # pragma-mark-obtain the current calendar time output format 15:41:55-(NSString *) getCurrentTime {NSDate * date = [NSDate date]; // The calendar is alendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; // indicates the NSDateComponents * compon component of a date object Ent = [calendar components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | region | NSCalendarUnitMinute | region fromDate: date]; // set the time zone [component setTimeZone: [NSTimeZone systemTimeZone]. NSInteger year = [component year]; NSInteger month = [component month]; NSInteger day = [component day]; NSInteger hour = [component hour]; NSInteger minute = [component Minute]; NSInteger second = [component second]; NSString * dateString = [NSString stringWithFormat: @ "% I-% @ % I % @ % 2i: % @ % 2i: % @ % I ", year, month> 10? @ "": @ "0", month, day> 10? @ "": @ "0", day, hour> 10? @ "": @ "0", hour, minute> 10? @ "": @ "0", minute, second> 10? @ "": @ "0", second]; return dateString ;}
2. output.
2015-10-22 14:06:16.920 getCurrentTime[11139:215609] -------getCurrentTime---2015-010-22 14:0 6:16