NSDate *now = [NSDate date];
//2.在该时间基础上加上10s
now = [now dateByAddingTimeInterval:10];
//3.获取当前时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger seconds = [zone secondsFromGMTForDate:now];
NSLog(@"%li",(long)seconds);
NSDate *newnow = [now dateByAddingTimeInterval:seconds];
NSLog(@"zone = %@", zone);
NSLog(@"newnow = %@", newnow);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒";
NSString *res = [formatter stringFromDate:now];
NSLog(@"%@", res);
NSString *str = @"2018年02月17日 15时13分55秒";
NSDateFormatter *newformatter = [[NSDateFormatter alloc] init];
newformatter.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒";
NSDate *newDate = [newformatter dateFromString:str];
NSLog(@"%@", newDate);
从所给的时间里面或获取元素
NSDate *now = [[NSDate alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitYear fromDate:now];
NSCalendarUnit枚举
NSDate *now = [[NSDate alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *cmps = [calendar components:type fromDate:now];
NSLog(@"%ld", cmps.year);
NSLog(@"%ld", cmps.month);
NSLog(@"%ld", cmps.day);
NSLog(@"%ld", cmps.hour);
NSLog(@"%ld", cmps.minute);
NSLog(@"%ld", cmps.second);
比较两个时间的差值
NSString *str = @"2018年02月17日 15时13分55秒";
NSDateFormatter *newformatter = [[NSDateFormatter alloc] init];
newformatter.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒";
NSDate *newDate = [newformatter dateFromString:str];
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *res= [calendar components:type fromDate:newDate toDate:now options:0];
NSLog(@"%@", res);