美文网首页
时间戳2016-4-29转成月份

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

作者: 清晰00 | 来源:发表于2016-04-29 22:55 被阅读193次

//时间转月 @"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;

}

相关文章

网友评论

      本文标题:时间戳2016-4-29转成月份

      本文链接:https://www.haomeiwen.com/subject/smufrttx.html