iOS获取当前时间和获取明天的日期

总结的几个关于时间的方法。

//获取当地时间

- (NSString*)getCurrentTime {NSDateFormatter*formatter = [[NSDateFormatteralloc] init];    [formatter setDateFormat:@"yyyy-MM-dd"];NSString*dateTime = [formatter stringFromDate:[NSDatedate]];returndateTime;}

//将字符串转成NSDate类型

- (NSDate*)dateFromString:(NSString*)dateString {NSDateFormatter*dateFormatter = [[NSDateFormatteralloc] init];    [dateFormatter setDateFormat:@"yyyy-MM-dd"];NSDate*destDate= [dateFormatter dateFromString:dateString];returndestDate;}


//传入今天的时间,返回明天的时间

- (NSString*)GetTomorrowDay:(NSDate*)aDate {NSCalendar*gregorian = [[NSCalendaralloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];NSDateComponents*components = [gregorian components:NSCalendarUnitWeekday|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDayfromDate:aDate];    [components setDay:([components day]+1)];NSDate*beginningOfWeek = [gregorian dateFromComponents:components];NSDateFormatter*dateday = [[NSDateFormatteralloc] init];    [dateday setDateFormat:@"yyyy-MM-dd"];return[dateday stringFromDate:beginningOfWeek];}

你可能感兴趣的:(iOS获取当前时间和获取明天的日期)