ITeye博客

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

修博龙泉 · 2012-10-29 10:09 · ios开发学习相关 · 移动开发 · Markdown


/**
 格式化时间
 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);
}


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。