-(NSString*)getCurrentTimes:(int)lastDay{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
[formatter setDateFormat:@"YYYY-MM-dd"];
//现在时间,你可以输出来看下是什么格式
NSDate *datenow = [NSDate date];
datenow = [datenow modifyDay:-lastDay];
//----------将nsdate按formatter格式转成nsstring
NSString *currentTimeString = [formatter stringFromDate:datenow];
return currentTimeString;
}
//延长或减少天数
-(NSDate *)modifyDay:(int)day{
return [self modifyYear:0 month:0 day:day];
}
写个NSDate 分类
//延长或减少年月日
-(NSDate *)modifyYear:(int)year month:(int)month day:(int)day{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[NSLocale currentLocale]];
NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
comps.day = comps.day + day;
comps.month = comps.month + month;
comps.year = comps.year + year;
return [calendar dateFromComponents:comps];
}
网友评论