时间戳2016-4-29转成月份

2016-04-29  本文已影响193人  清晰00

//时间转月 @"1992-12-22"

- (NSString *)timeWithMonth:(NSString *)time

{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date = [formatter dateFromString:time];

NSDateFormatter *ff = [[NSDateFormatter alloc] init];

ff.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];

NSArray *arr = ff.monthSymbols;

[ff setDateFormat:@"MM"];

NSInteger index = [[ff stringFromDate:date] integerValue];

NSString *month = [arr objectAtIndex:index-1];

NSDate *currentDate = [NSDate date];//获取当前时间,日期

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-Hans"];

NSArray *arr2 = dateFormatter.monthSymbols;

[dateFormatter setDateFormat:@"MM"];

NSInteger index2 = [[dateFormatter stringFromDate:currentDate] integerValue];

NSString *month2 = [arr2 objectAtIndex:index2-1];

if ([month isEqualToString:month2]) {    //比较当前时间

return @"本月";

}

return month2;

}

上一篇 下一篇

猜你喜欢

热点阅读