首页 > 移动平台 > 详细

iOS规范化时间格式,object-C计算指定时间与当前的时间差

时间:2015-08-12 13:06:01      阅读:207      评论:0      收藏:0      [点我收藏+]

object-c计算指定时间与当前的时间差

头文件(.h):

#import <Foundation/Foundation.h>

@interface LuDate : NSDate
+(NSString *) compareCurrentTime:(NSString*) strDate;
@end

.m文件:

/**
 
 * 计算指定时间与当前的时间差
 
 * @param compareDate   某一指定时间
 
 * @return 多少(秒or分or天or月or年)+前 (比如,3天前、10分钟前)
 
 */

+(NSString *) compareCurrentTime:(NSString*) strDate

{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *compareDate= [dateFormatter dateFromString:strDate];
    
    
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    
    timeInterval = -timeInterval;
    
    long temp = 0;
    
    NSString *result;
    
    if (timeInterval < 60) {
        
        result = [NSString stringWithFormat:@"刚刚"];
        
    }
    
    else if((temp = timeInterval/60) <60){
        
        result = [NSString stringWithFormat:@"%ld分前",temp];
        
    }
    
    
    
    else if((temp = temp/60) <24){
        
        result = [NSString stringWithFormat:@"%ld小前",temp];
        
    }
    
    
    
    else if((temp = temp/24) <30){
        
        result = [NSString stringWithFormat:@"%ld天前",temp];
        
    }
    
    
    
    else if((temp = temp/30) <12){
        
        result = [NSString stringWithFormat:@"%ld月前",temp];
        
    }
    
    else{
        
        temp = temp/12;
        
        result = [NSString stringWithFormat:@"%ld年前",temp];
        
    }
    
    
    
    return  result;
    
}
@end

 

iOS规范化时间格式,object-C计算指定时间与当前的时间差

原文:http://www.cnblogs.com/luerniu/p/4723731.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!