【代碼筆記】iOS-擷取現在的日曆時間,筆記ios-擷取日曆
一,代碼。
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"-------getCurrentTime---%@",[self getCurrentTime]); }#pragma -mark -擷取現在的日曆時間 輸出形式 2014-06-27 15:41:55- (NSString *) getCurrentTime{ NSDate* date = [NSDate date]; //日曆為陽曆 NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; //表示一個日期對象的組件 NSDateComponents *component = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date]; //設定時區 [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-%@%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; }
二,輸出。
2015-10-22 14:06:16.920 getCurrentTime[11139:215609] -------getCurrentTime---2015-010-22 14:0 6:16