一,代码。
- (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