object-c 获得时间/时间戳 格式化时间

/**
 格式化时间
 timeSeconds 为0时表示当前时间,可以传入你定义的时间戳
 timeFormatStr为空返回当当时间戳,不为空返回你写的时间格式(yyyy-MM-dd HH:ii:ss)
 setTimeZome ([NSTimeZone systemTimeZone]获得当前时区字符串)
 */
-(NSString *)setTimeInt:(NSTimeInterval)timeSeconds setTimeFormat:(NSString *)timeFormatStr setTimeZome:(NSString *)timeZoneStr{
    
    NSString *date_string;
    
    NSDate *time_str;
    if( timeSeconds>0){
        time_str =[NSDate dateWithTimeIntervalSince1970:timeSeconds];
    }else{
        time_str=[[NSDate alloc] init];
    }
    
    if( timeFormatStr==nil){
        date_string =[NSString stringWithFormat:@"%d",(long)[time_str timeIntervalSince1970]];
    }else{
        NSDateFormatter *date_format_str =[[[NSDateFormatter alloc] init] autorelease];
        [date_format_str setDateFormat:timeFormatStr];
        if( timeZoneStr!=nil){
            [date_format_str setTimeZone:[NSTimeZone timeZoneWithName:timeZoneStr]];
        }
        date_string =[date_format_str stringFromDate:time_str];
    }
    
    return date_string;
}
 
/**
 *用法
*/
-(void)viewWillAppear:(BOOL)animated{
    
    NSString *a =[self setTimeInt:1317914496 setTimeFormat:@"yy.MM.dd HH:mm:ss" setTimeZome:nil];
    NSString *b =[self setTimeInt:0 setTimeFormat:@"yy.MM.dd HH:mm:ss" setTimeZome:nil];
    NSString *c =[self setTimeInt:0 setTimeFormat:nil setTimeZome:nil];
    NSString *d =[self setTimeInt:0 setTimeFormat:@"yy.MM.dd HH:mm:ss" setTimeZome:@"GMT"];
    
    NSLog(@"%@,,,%@,,,%@,,,%@",a,b,c,d);
}

猜你喜欢

转载自longquan.iteye.com/blog/1707118
今日推荐